gpt4 book ai didi

android - Jetpack Compose 中的深层链接导航

转载 作者:行者123 更新时间:2023-12-05 01:27:07 24 4
gpt4 key购买 nike

我想对 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>

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)
}
}

如果我现在单击相应的链接,应用程序会打开但导航不起作用

最佳答案

问题出在 Activity launchMode 上:

The documentation表示强烈建议在使用导航时始终使用 standard 的默认 launchMode。使用标准启动模式时,Navigation 会通过调用 handleDeepLink() 自动处理深层链接。处理 Intent 中任何显式或隐式深层链接。但是,如果在使用备用 launchMode(例如 singleTop)时重新使用 Activity,这不会自动发生。这种情况下,需要在onNewIntent()中手动调用handleDeepLink(),如下例所示:

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
navController.handleDeepLink(intent)
}

关于android - Jetpack Compose 中的深层链接导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69781886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com