Anthony
09/02/2021, 7:38 PMandroidx.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.Anthony
09/02/2021, 7:40 PMYou 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 errorAnthony
09/02/2021, 7:41 PMIan Lake
09/02/2021, 7:53 PMAnthony
09/03/2021, 8:16 PMval 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.Ian Lake
09/03/2021, 8:20 PMcomposable
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 hierarchyAnthony
09/03/2021, 8:23 PM@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?Ian Lake
09/03/2021, 8:53 PMIan Lake
09/03/2021, 8:55 PMNavHost
works, so I'm not sure how that is related to the error you posted aboutAnthony
09/03/2021, 9:36 PMIan Lake
09/03/2021, 9:39 PM