Yariv Ziporin
01/02/2023, 4:28 PMagrosner
01/02/2023, 4:34 PMColton Idle
01/02/2023, 4:59 PMColton Idle
01/02/2023, 5:07 PM@Composable
fun SignUpScreen(
sharedViewModel: LoggedOutScreensSharedViewModel,
viewModel: SignUpScreenViewModel = hiltViewModel(),
)
and
@Composable
fun LoginScreen(
sharedViewModel: LoggedOutScreensSharedViewModel,
viewModel: LoginScreenViewModel = hiltViewModel(),
)
and
fun NavGraphBuilder.loginGraph(navController: NavController, activity: Activity?) {
navigation(startDestination = Screen.SignUp.route, route = Screen.LoggedOutScreens.route) {
composable(Screen.SignUp.route) {
val sharedVM = remember() { navController.getBackStackEntry(Screen.LoggedOutScreens.route) }
SignUpScreen(
sharedViewModel = hiltViewModel(sharedVM)
)
}
composable(Screen.Login.route) {
val shared = remember() { navController.getBackStackEntry(Screen.LoggedOutScreens.route) }
LoginScreen(sharedViewModel = hiltViewModel(shared))
}
}
}
Seems to work fineYariv Ziporin
01/02/2023, 8:19 PMmattinger
01/03/2023, 12:04 AMval viewModel = viewModel(.....)
val navController = rememberNavController()
NavHost(...) {
}
The view model would effectively be scoped to the composable that owns the navcontroller as well, so it could be shared across the screens in your nav graph. Personally, i prefer to do this sort of thing using the activity as the ViewModelStoreOwner instead of any particular composable. This allows the view model to survive rotation and configuration changes. It's not entirely clear to me if a viewModel like above would give that to you.mattinger
01/03/2023, 12:06 AM