I was trying to follow this video for my login flo...
# compose
r
I was trying to follow this video for my login flow.

https://www.youtube.com/watch?v=09qjn706ITA&t=285s&ab_channel=AndroidDevelopersfor

But I am facing a problem. Details in the thread.
1
On my homepage, I am using the following code. Here
viewmodel.user
is a MutableState.
Copy code
val loginState = savedStateHandle.getLiveData<Boolean>("LOGIN_SUCCESS").observeAsState()

if (viewModel.user.value == null) {
    LaunchedEffect(viewModel.user.value) {
        navController.navigate(Screens.Login.id)
    }
}
when (loginState.value) {
    false -> {
        onFinish()
    }
    true -> {
        MainContent()
    }
}
And In Login Screen
Copy code
AuthUiState.LoggedIn -> {
    savedStateHandle?.set("LOGIN_SUCCESS", true)
    navController.popBackStack()
}
But the problem is when I use popBackStack() and go back to my homepage the following block runs again and go back to the login page again because of
viewModel.user.value
is still null. Since I am using two different viewModels
LoginViewModel
and
HomeViewModel
so I can't update
viewModel.user
value directly from the
LoginViewModel
right after the successful login instead, I update it from the
MainContent()
in
HomeViewModel
.
Copy code
if (viewModel.user.value == null) {
    LaunchedEffect(viewModel.user.value) {
        navController.navigate(Screens.Login.id)
    }
}
What is the appropriate way of updating user value when using two different ViewModel? So that the navigation block doesn't execute again when I pop back to my homepage.