Hello, I’ve been experiencing some crashes lately ...
# compose
a
Hello, I’ve been experiencing some crashes lately when trying to pop a destination that, seemingly from the pattern, contains a nested navigation (either a NavHost or an AnimatedNavHost). It’s almost like when the screen containing the navhost is popped off the stack, it’s attempting to recompose during the time it’s being popped, and throws the following exception:
Copy code
java.lang.IllegalStateException: You cannot access the NavBackStackEntry's ViewModels after the NavBackStackEntry is destroyed.
        at androidx.navigation.NavBackStackEntry.getViewModelStore(NavBackStackEntry.kt:207)
        at com.google.accompanist.navigation.animation.AnimatedNavHostKt.AnimatedNavHost(AnimatedNavHost.kt:144)
        at com.google.accompanist.navigation.animation.AnimatedNavHostKt$AnimatedNavHost$11.invoke(Unknown Source:23)
        at com.google.accompanist.navigation.animation.AnimatedNavHostKt$AnimatedNavHost$11.invoke(Unknown Source:10)
        at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:140)
        at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2158)
        at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2404)
        at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2585)
        at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2571)
        at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:247)
        at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1)
        at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2571)
        at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:2547)
        at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:620)
        at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:786)
        at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:105)
This is on latest 2.5.0-alpha01, but was also occurring on 2.4.0-alpha01 (but with a different error message)
If what I’m guessing is true, and the NavHost is recomposing even though its navBackStackEntry’s have all been moved to the destroyed state, I’m not entirely sure how to workaround this issue
I’ve found these bugs: https://android-review.googlesource.com/c/platform/frameworks/support/+/1806636 https://android-review.googlesource.com/c/platform/frameworks/support/+/1896853 which all seem fairly similar to what I’m experiencing I’ve also seen this issue with Accompanist’s Navigation-material bottomSheet() where if we swipe to dismiss the bottom sheet containing a destination that contains a NavHost or AnimatedNavHost, the same crash occurs. That is, when the bottomSheet is being popped (and I believe in the bottomSheet’s case, it uses
popWithTransition
but not entirely sure if that’s the cause), then the destination being popped’s navhost seemingly tries to recompose, which leads it to run L:144 in the AnimatedNavHost code`
Copy code
navController.setViewModelStore(viewModelStoreOwner.viewModelStore)
which when retrieving the viewModelStore from it’s backStackEntry, it has already moved to the
DESTROYED
state, thus leading to the IllegalStateException
i
If you have a repro project, file a bug
e
I too have experienced this issue; I came across: • https://stackoverflow.com/questions/68752739/jetpack-compose-navigation-illegalstateexceptionhttps://issuetracker.google.com/issues/198741720https://www.reddit.com/r/androiddev/comments/rtfmlf/javalangillegalstateexception_you_cannot_access/ All stackoverflow links suggested downgrading to navigation:2.4.0-alpha07 or 5. None of which really fixed it. Even tried the latest 2.5.0-alpha02 with no luck. From the reddit thread it sounds like Google is choosing not to fix it which is wild. Also a lot of the issues filed keep getting closed claiming their fixed with they arent
i
No issues have ever been filed against Navigation 2.5 for this issue. If you have a repro project, file a bug as we'd love to fix any issue you are still experiencing
e
Sorry Ian, I missed spoke. The last sentence was what the reddit post was referring to. Not a claim by me. But yes, im compiling a slimmed down sample repo to show off the issue. Will file a bug
s
hey @Andrew Leung have you managed to resolve this?
a
It's been awhile since I looked into this. I think there was some issue on our end with respect to calling clearBackstack on the NavController. We thought calling that would help us return to the top of the stack for better control, but it was causing us these race conditions.