I have flow of
Login -> Home -> Profile -> Login (Logout)
Login -> Register -> SetupProfile -> Home -> Profile -> Login (Logout)
I made my code like below
@Composable
fun TestNavigation(
modifier: Modifier = Modifier,
mainViewModel: MainViewModel,
navController: NavHostController = rememberNavController(),
) {
val currentUser by mainViewModel.currentUser.collectAsState()
val startRoute = if (currentUser is MainViewModel.UserState.NotAuthorized) {
Screen.Login.route
} else {
Screen.Home.route
}
NavHost(
modifier = modifier,
navController = navController,
startDestination = startRoute,
) {
composable(
route = Screen.Login.route,
) {
LoginScreen(
onNavigateHome = { navController.navigate(Screen.Home.route),
onNavigateRegister = { navController.navigate(Screen.Register.route) },
)
composable(
route = Screen.Register.route,
) {
SignupScreen(
onNavigateSetupProfile = { navController.navigate(Screen.SetupProfile.route) },
onNavigateLogin = {
navController.popBackStack(
route = Screen.Login.route,
inclusive = false
)
}
)
}
composable(
route = Screen.SetupProfile.route,
) {
SetProfileScreen(
onNavigateHome = {
navController.navigate(Screen.Home.route) {
popUpTo(Screen.Login.route) { inclusive = true }
}
},
)
}
composable(
route = Screen.Home.route,
) {
if (currentUser is MainViewModel.UserState.Authorized) {
MainPageScreen(//The rest of the code)
}
}
}
The problem is the flow
Expected: Login -> Register -> SetupProfile -> Home -> Profile -> Login (Logout)
Result: Login -> Register -> Home -> Profile -> Login (Logout)
The SetupProfile is skipped to Home.
I tried testing it the start navigation using Login only or Home only it doesn’t work as expected also which is normal.
I tried to make a LaunchEffect outside the NavHost by checking current user then if it’s not authorized it will navigate to login, but this causes problem on logout.
Michelle – is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.