How can I make out of many only two or more specif...
# compose
r
How can I make out of many only two or more specific composable screens in the Navigation graph share the same ViewModel?
I thought this would work. But It doesn't work.
Copy code
fun MyApp() {
    NavHost(navController, startDestination = "Parent") {
        navigation(
            startDestination = exampleWithRoute,
            route = "Parent"
        ) {
            composable("exampleWithRoute") { backStackEntry ->
                val parentViewModel = hiltViewModel<ParentViewModel>(
                    navController.getBackStackEntry("Parent")
                )
                ExampleWithRouteScreen(parentViewModel)
            }
            composable("exampleWithRoute2") { backStackEntry ->
                val parentViewModel = hiltViewModel<ParentViewModel>(
                    navController.getBackStackEntry("Parent")
                )
                ExampleWithRouteScreen2(parentViewModel)
            }
        }
    }
}
i
That certainly should work
r
Ops Now It is actually working. But the problem is when I am using this line in my imaginary ExampleWithRouteScreen2 Screen. See original code below
Copy code
navController.navigate(Screens.Home.id) {
    popUpTo(Screens.Authenticate.id)
}
I am getting this java.lang.IllegalArgumentException: No destination with route authenticate is on the NavController's back stack. The current destination is Destination(0x78d845ec) route=home
This is the original code. I am using AnimatedNavHost and I have removed the animation part from the code.
Copy code
AnimatedNavHost(
    navController = navController,
    startDestination = Screens.Authenticate.id
) {
    navigation(
        startDestination = Screens.Login.id,
        route = Screens.Authenticate.id
    ) {

        composable(
            route = Screens.Login.id
        ) {
            val viewModel = hiltViewModel<LoginViewModel>(
                navController.getBackStackEntry(Screens.Authenticate.id)
            )
            Login(navController = navController, viewModel)
        }

        composable(route = Screens.EnterPin.id) {
            val viewModel = hiltViewModel<LoginViewModel>(
                navController.getBackStackEntry(Screens.Authenticate.id)
            )
            EnterPin(navController = navController, viewModel = viewModel)
        }
    }

    composable(route = Screens.Home.id) {
        val viewModel = hiltViewModel<HomeViewModel>()
        MainContent(navController = navController, viewModel)
    }
}
i
There's two problems here: 1) you need to wrap any calls to
getBackStackEntry
in a
remember
- next week's alpha08 release adds a lint check for that 2) Nav graph scoped ViewModels are destroyed too soon (when the animation starts, rather than when it ends) - that's https://issuetracker.google.com/issues/194313238 which is also fixed for alpha08
🦜 1
Unfortunately, there's no good workaround for that second one besides using a snapshot build of Navigation (from the directions at http://androidx.dev)
r
Ok thanks . Then I guess I am waiting for alpha08.