Jetpack Compose 中的深层链接导航

Deep Links navigation in Jetpack Compose

我想将 deep links 与 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)
    }
}

如果我现在点击相应的 link 应用程序打开但导航不起作用

我通过删除

让它工作
android:launchMode="singleInstance"

来自清单

问题出在 Activity launchMode:

The documentation says that is strongly recommended to always use the default launchMode of standard when using Navigation. When using standard launch mode, Navigation automatically handles deep links by calling handleDeepLink() 处理 Intent 中的任何显式或隐式深层链接。但是,如果在使用备用 launchMode(例如 singleTop)时重新使用 Activity,则不会自动发生这种情况。这种情况下,需要在onNewIntent()中手动调用handleDeepLink(),如下例所示:

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