https://kotlinlang.org logo
#navigation-architecture-component
Title
# navigation-architecture-component
k

Kshitij Patil

12/07/2020, 7:19 PM
Im passing navigation argument from one fragment to other. I want to fetch some details based on the given args, in the ViewModel of destination fragment. This results in modification to the state in viewmodel which is being observed by the fragment. Doing so causes
Copy code
java.lang.IllegalArgumentException: No destination with ID 2131362278 is on the NavController's back stack. The current destination is Destination(ai.adagrad.gateguard:id/navigation_add_visitor) label=AddVisitorFragment class=ai.adagrad.gateguard.addvisitor.AddVisitorFragment
        at androidx.navigation.NavController.getBackStackEntry(NavController.java:1295)
       $inlined$navGraphViewModels$1.invoke(NavGraphViewModelLazy.kt:56)
        $inlined$navGraphViewModels$1.invoke(NavGraphViewModelLazy.kt)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        $inlined$navGraphViewModels$2.invoke(NavGraphViewModelLazy.kt:59)
    $inlined$navGraphViewModels$2.invoke(NavGraphViewModelLazy.kt)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:53)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at androidx.lifecycle.DispatchQueue.drainQueue(DispatchQueue.kt:75)
        at androidx.lifecycle.DispatchQueue.enqueue(DispatchQueue.kt:107)
        at androidx.lifecycle.DispatchQueue.runOrEnqueue(DispatchQueue.kt:97)
        at androidx.lifecycle.PausingDispatcher.dispatch(PausingDispatcher.kt:182)
        at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:305)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:27)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:167)
        at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
        at androidx.lifecycle.PausingDispatcherKt$whenStateAtLeast$2.invokeSuspend(PausingDispatcher.kt:162)
        at androidx.lifecycle.PausingDispatcherKt$whenStateAtLeast$2.invoke(PausingDispatcher.kt)
        at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:91)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:161)
        at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
        at androidx.lifecycle.PausingDispatcherKt.whenStateAtLeast(PausingDispatcher.kt:156)
        at androidx.lifecycle.PausingDispatcherKt.whenCreated(PausingDispatcher.kt:42)
        at androidx.lifecycle.LifecycleCoroutineScope$launchWhenCreated$1.invokeSuspend(Lifecycle.kt:74)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:342)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:27)
        at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
        at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:49)
        at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
        at androidx.lifecycle.LifecycleCoroutineScope.launchWhenCreated(Lifecycle.kt:73)
        at androidx.fragment
This exception. Without updating any state, navigation works fine and I'm even able to view the argument on the UI, but the moment I pass it to the ViewModel, crash occurs Edit: To give you more context, this fragment is hosting a FragmentContainerView
@Ian Lake any thoughts why this could be happening ?
a

allan.conda

12/07/2020, 11:19 PM
The cause is clear from the stacktrace. The navigation is no longer in the backstack when you try to access it.
I don’t see any blocker from your side here. You could find the cause by using breakpoints
k

Kshitij Patil

12/08/2020, 3:33 AM
But as I said, Navigation works properly when I don't invoke the ViewModel methods so the Navigation is indeed in the backstack but seems like the state has been lost. I tried
savedInstanceState
and
restoreInstanceState
but that too didn't work
I fixed it. I was trying to create a Nav Graph scoped viewmodel in a fragment that is not part of the navgraph but instead hosting it (through
FragmentContainerView
)
26 Views