android - 使用 App Links 跳转会关闭 CustomTab
问题描述
所以我想要实现的是以下几点:
在 App 1 中,我在 CustomTab 中加载一个网站,该网站通过 App Links 跳转到 App 2。在 App 2 中,执行任务后,我想跳回 App 1,其中 CustomTab 仍然需要打开,因为它需要进行一些处理。我有这个工作,我通过包名称跳回 App 1,但我也需要通过 App Links 让它工作。
但是,当前发生的情况是,当我通过 App Links 跳转回来时,CustomTab 似乎已关闭。我的第一个想法是我没有正确保留应用程序/启动活动。
应用程序 1 的 AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.secret.package.name">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="some.secret.url" />
</intent-filter>
</activity>
</application>
</manifest>
在 App 1 中打开 CustomTab:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));
应用程序 2 中的代码跳回:
private void performAppSwitch() {
Intent intentWithURI = new Intent(Intent.ACTION_VIEW, Uri.parse(APP_LINKS_URL));
PackageManager packageManager = context.getPackageManager();
if (intentWithURI.resolveActivity(packageManager) != null) {
intentWithURI.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentWithURI);
} else {
Intent intentWithPackageID = packageManager.getLaunchIntentForPackage(PACKAGE_NAME);
if (intentWithPackageID != null) {
List<ResolveInfo> activities = packageManager.queryIntentActivities(intentWithPackageID, 0);
boolean isIntentSafe = !activities.isEmpty();
if (isIntentSafe) {
intentWithPackageID.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentWithPackageID);
}
}
}
}
关于为什么当我通过包名称切换而不是通过应用程序链接切换时它为什么工作的任何想法?我已经尝试了所有可能的 launchMode 变体
解决方案
问题是 CustomTab 正在启动它自己的活动,我必须确保 App 1 没有重绘。
因此,在 App 1 中,将以下内容添加到我的主要活动的 onCreate 中就可以了:
if (!isTaskRoot()) {
finish();
return;
}
推荐阅读
- c# - 如何使用 .net 设置 cmd 或 powershell 控制台位置?
- r - 如何使用带有 R 的 strsplit 的字符串分隔符拆分文本?
- angular - 多个字段的自动完成在所有字段中显示相同的选项
- python - 如何自动化 AWS s3 存储桶对象的权限
- foreign-keys - Symfony4:具有外键和截断/重置自动增量值的 DataFixtures
- azure-iot-hub - Azure IOT SDK C# GroupCertificateVerification 不起作用?
- r - R optim() L-BFGS-B 需要 'fn' 的有限值
- c# - C# webclient 或 webbrowser
- java - 使用改造上传图片
- maven - mvn clean compile assembly:single 工作,但 mvn install 或 package 无法创建具有依赖项的 jar