Has anyone tried out `androidx.navigation:navigati...
# compose
a
Has anyone tried out
androidx.navigation:navigation-compose:2.4.0-alpha08
yet? I seem to be getting an error every time I navigate back to a composable using a navigation graph scope viewmodel.
👍 1
I keep getting
You cannot access the NavBackStackEntry's ViewModels until it is added to the NavController's back stack (i.e., the Lifecycle of the NavBackStackEntry reaches the CREATED state)
as an error
This is the way I am implementing it
i
That looks like the kind of error that should be fixed in alpha08. If you can reproduce it in a sample project, please file an issue and we can take a look: https://issuetracker.google.com/issues/new?component=409828
a
After some debugging I came across what seemed to be an issue, I figure I might as well ask you @Ian Lake before filing a bug to see if my technique is wrong. The error is thrown because in my nav graph I have these two lines in the composable.
Copy code
val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentDestination = navBackStackEntry?.destination
After commenting that out, the code works properly. It seems that trying to access the current destination dosen't work.
i
I think I'm missing some context? Within a
composable
lambda, you should using the
NavBackStackEntry
that is passed to you: that is your current back stack entry. You shouldn't be using anything but that within that lambda or anything in that Compose hierarchy
a
Currently my code looks like this,
Copy code
@Composable
fun NavGraph(){
    val navController = rememberNavController()
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentDestination = navBackStackEntry?.destination

    LaunchedEffect(currentDestination) {
        println("CURR dest: ${currentDestination?.route}")
       if (currentDestination?.route
           == "${Screen.ChapterVideoPlayerScreen.route}/{chapterId}?startSpreadIndex={startSpreadIndex}"
        ) {
            onImmersiveMode(true)
        } else {
            onImmersiveMode(false)
        }
    }

    NavHost(
        navController = navController,
        startDestination = Screen.Screen1.route,
    ) {

...

}
Based on the destination, the composable returns a callback which indicates whether we turn immersive mode on or off. Would this not be the right way to do so?
i
That is fine (although personally I'd look at using an argument based approach rather than looking at routes): https://kotlinlang.slack.com/archives/CJLTWPH7S/p1628588548153300?thread_ts=1628541488.118700&cid=CJLTWPH7S
But none of that code has any effect on how the code within your
NavHost
works, so I'm not sure how that is related to the error you posted about
a
That where I am confused myself, It doesn't seem like it should have any impact on the navigation of my navhost composable. It worked before in Alpha07, but no longer works in Alpha08. I replicated this in a sample repo, and created an issue tracker here. https://issuetracker.google.com/issues/198741720
i
Thanks for filing the issue. We'll take a look 🙂
👍 1