Mr.*_*ine 9 android android-deep-link android-jetpack-navigation android-jetpack-compose
我想使用 Jetpack Compose 的 Nav Host 的深层链接,并按照 Compose Navigation 上的此页面进行操作: https: //developer.android.com/jetpack/compose/navigation#deeplinks
我的实现:AndroidManifest.xml:
<application ...>
<activity
...
android:allowTaskReparenting="true"
android:launchMode="singleInstance">
...
<intent-filter>
<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="xkcd.com" />
<data android:scheme="https" android:host="www.xkcd.com" />
</intent-filter>
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
MainActivity.onCreate().setContent{}
val rootUri = "https://www.xkcd.com"
NavHost(navController = navController, startDestination = "mainView") {
composable("mainView", deepLinks = listOf(navDeepLink { uriPattern = rootUri })) {
MainContent()
}
composable(
route = "singleView/{number}",
arguments = listOf(navArgument("number") { type = NavType.IntType }),
deepLinks = listOf(navDeepLink { uriPattern = "$rootUri/{number}" })
) { backStackEntry ->
val number = backStackEntry.arguments?.getInt("number")
SingleView(number)
}
}
Run Code Online (Sandbox Code Playgroud)
如果我现在单击相应的链接,应用程序会打开,但导航不起作用
Nen*_*eno 13
问题出在 Activity launchMode上:
该文档表示,强烈建议在使用导航时始终使用默认launchMode值standard。当使用标准启动模式时,Navigation 通过调用handleDeepLink()来自动处理深层链接,以处理 Intent 中的任何显式或隐式深层链接。但是,如果在使用替代方案(launchMode例如singleTop. 这种情况下,需要手动调用handleDeepLink(),onNewIntent()如下例:
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
navController.handleDeepLink(intent)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10232 次 |
| 最近记录: |