提问者:小点点

从通知导航到带有NavController的片段


当用户单击通知时,我需要导航到一个片段。

如果应用程序正在运行,这可以正常工作。但是,当我退出应用程序,然后单击通知时,它会崩溃,并显示以下错误消息:

2020-04-24 18:06:49.607 14832-14832/com.package.name E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.package.name, PID: 14832
java.lang.IllegalStateException: no current navigation node
    at androidx.navigation.NavController.navigate(NavController.java:824)
    at androidx.navigation.NavController.navigate(NavController.java:804)
    at androidx.navigation.NavController.navigate(NavController.java:790)
    at androidx.navigation.NavController.navigate(NavController.java:985)
    at com.package.name.core.navigation.NavigationActivity.handleNewIntent(NavigationActivity.kt:358)
    at com.package.name.core.navigation.NavigationActivity.onPostCreate(NavigationActivity.kt:257)
    at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1385)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3088)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6863)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我的导航代码:

navController.navigate(
                navigateToFragment(
                    someParam = "some value",
                    someOtherParam = "other value"
                )
            )

如何实例化我的navController

private val navController: NavController by lazy {
    Navigation.findNavController(this, R.id.nav_host_fragment)
}

我想这是因为我的导航控制器没有正确初始化?有什么想法可以解决这个问题/正确的方法是什么?


共1个答案

匿名用户

我找到了解决办法。

缺少的是为我的navcontroller设置开始目标,如下所示:

  graph = navGraph.apply { startDestination = destinationId }
  currentDestination?.let { applyDestinationPropertiesToToolbar(it) }