Android中通过App Link打开App1时如何恢复Chrome Custom Tab
问题场景
- 打开App1
- App1启动Chrome Custom Tab(作为新Activity推入App1的任务栈)
- Chrome Custom Tab通过Android App Link打开App2
- App2通过Android App Link打开App1
当前问题:App2唤起App1时,Chrome Custom Tab会消失,需要恢复该CCT活动(或App1最近的Activity)
解决方案
你推测给Chrome Custom Tab的Activity加intent-filter的思路不可行,因为CCT是Chrome的系统组件,你无法修改它的AndroidManifest配置。正确的做法是通过控制App1的启动模式和App2唤起Intent的标志来实现:
1. 配置App1接收App Link的Activity
在App1的AndroidManifest.xml中,找到处理App Link的目标Activity,设置launchMode="singleTask",确保App Link唤起时复用现有任务栈,而不是新建实例:
<activity android:name=".YourAppLinkHandlerActivity" android:launchMode="singleTask"> <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" /> <!-- 替换成你的App Link域名和路径 --> <data android:scheme="https" android:host="your-app-domain.com" android:pathPrefix="/target-path" /> </intent-filter> </activity>
2. 配置App2唤起App1的Intent
在App2中构建唤起App1的Intent时,添加FLAG_ACTIVITY_REORDER_TO_FRONT和FLAG_ACTIVITY_NEW_TASK标志,强制将App1的现有任务栈带到前台,栈顶的Chrome Custom Tab会自动恢复显示:
// Kotlin示例 val appLinkUri = Uri.parse("https://your-app-domain.com/target-path") val intent = Intent(Intent.ACTION_VIEW, appLinkUri) intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or Intent.FLAG_ACTIVITY_NEW_TASK) startActivity(intent)
// Java示例 Uri appLinkUri = Uri.parse("https://your-app-domain.com/target-path"); Intent intent = new Intent(Intent.ACTION_VIEW, appLinkUri); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
原理说明
Chrome Custom Tab是通过App1的Context启动的,默认会被推入App1的任务栈中。当通过上述配置唤起App1时,系统会直接将App1的现有任务栈带到前台,栈顶的CCT自然会成为当前显示的Activity,不会被销毁或隐藏。
内容的提问来源于stack exchange,提问作者Insectivore
相关产品推荐
相关产品推荐

