Has anybody tried `navigation-compose 2.4.0-alpha0...
# compose
j
Has anybody tried
navigation-compose 2.4.0-alpha05
? There must have been some behavior changes because my navigation logic now leads to infinite loops which didn’t happen with alpha04.
i
Quite a bit has changed on alpha05, I'd definitely file a bug with a sample project if you're seeing something unexpected
j
Right now I’m seeing infinite loops due to a navigation action I had attached to a
SideEffect
. Need to debug more to create something reproducible.
Yeah, for some reason a
SideEffect
which used to be triggered only once is now being trigged multiple times, perhaps this is because of the added crossfade navigation animation?
i
The whole difference between
SideEffect
and other effects like
LaunchedEffect
is that side effect runs every composition, so that doesn't seem like the right tool for what you want
Of course, you could wrap your navigate call in
if (backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED)
- the Lifecycle gets moved down immediately once you call navigate the first time
j
Indeed, though it looks like animations now trigger multiple recompositions (which is expected) so they exposed this flaw of my code.
I have found another behavior change in alpha05. ViewModel scoped to a sub navgraph will crash when doing
navigateUp()
and the
viewModel<>()
method is called within the
composable()
lambda.
Copy code
NavHost(
                        navController = navController,
                        startDestination = "a",
                        modifier = Modifier.fillMaxSize(),
                        route = "mainGraph",
                    ) {
                        composable(route = "a") {
                            ScreenA { navController.navigate("b") }
                        }
                        navigation(
                            startDestination = "b",
                            route = "subGraph",
                        ) {
                            composable(route = "b") {
                                val vm = viewModel<SomeViewModel>(
                                    viewModelStoreOwner = navController.getBackStackEntry("subGraph")
                                )
                                ScreenB { navController.navigateUp() }
                            }
                        }
                    }
Error is:
No destination with route subGraph is on the NavController's back stack. The current destination is Destination(0xe5901274) route=a
Is it me doing something wrong or is this something I should file in a bug report?
i
Well, it certainly makes sense that when you navigateUp (popping that destination and it's graph) that the graph wouldn't exist anymore, but you should file an issue for that as we should keep that around until the transition completes
👌 2
j
FYI: As a workaround I had to move the call to
viewModel()
inside the screen composable. Since I didn’t want to make the screen composable dependent on
NavController
I had to use
LocalViewModelStoreOwner
to indirectly propagate the owner tied to the subgraph:
Copy code
composable(
                route = "b",
              ) {
                val owner = remember { navController.getBackStackEntry("subGraph") }
                CompositionLocalProvider(
                  LocalViewModelStoreOwner provides owner
                ) {
                  ScreenB { navController.navigateUp() }
                }
              }
p
It also crashes for me using
popUpTo
navOption
, probably same root cause
i
Please file separate issues with projects that reproduce your individual issues, it is much, much easier to dup issues than untangle completely separate issues
p
done, updated minimum code project to reproduce the issue
👍 1