【问题标题】:Deep Links navigation in Jetpack ComposeJetpack Compose 中的深层链接导航
【发布时间】:2021-12-15 07:07:06
【问题描述】:

我想在 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)
    }
}

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

【问题讨论】:

  • 检查日志信息。可能有一个表示无法识别的路径。
  • @Johann 没有日志消息似乎与深层链接或导航有任何关系

标签: android android-jetpack-compose android-jetpack-navigation android-deep-link


【解决方案1】:

问题出在 Activity launchMode:

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

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

【讨论】:

    【解决方案2】:

    我通过删除让它工作了

    android:launchMode="singleInstance"
    

    来自清单

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多