https://kotlinlang.org logo
Title
j

julioromano

07/22/2021, 1:24 PM
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

Ian Lake

07/22/2021, 1:31 PM
Quite a bit has changed on alpha05, I'd definitely file a bug with a sample project if you're seeing something unexpected
j

julioromano

07/22/2021, 1:33 PM
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

Ian Lake

07/22/2021, 2:04 PM
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

julioromano

07/22/2021, 2:05 PM
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.
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

Ian Lake

07/22/2021, 2:12 PM
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

julioromano

07/22/2021, 2:28 PM
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:
composable(
                route = "b",
              ) {
                val owner = remember { navController.getBackStackEntry("subGraph") }
                CompositionLocalProvider(
                  LocalViewModelStoreOwner provides owner
                ) {
                  ScreenB { navController.navigateUp() }
                }
              }
p

pepos

07/22/2021, 5:53 PM
It also crashes for me using
popUpTo
navOption
, probably same root cause
i

Ian Lake

07/22/2021, 6:24 PM
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

pepos

07/22/2021, 8:28 PM
done, updated minimum code project to reproduce the issue
👍 1