Shun Takahashi
Shun Takahashi

Reputation: 151

The value in remember { mutableStateOf()} can be hold after I finished the app?

I am developing the navigation flow of login by Jetpack compose. Then I don't understand the functions.

@Composable
fun CheckSignedIn(vm: AuthViewModel,navController: NavController){
    val alreadyLoggedIn = remember { mutableStateOf(false)}
    val signedIn = vm.signedIn.value
    if(signedIn && !alreadyLoggedIn.value){
        alreadyLoggedIn.value = true
        navController.navigate(DestinationScreen.Feed.route){
            popUpTo(0)
        }
    }
}
class AuthViewModel @Inject constructor(
    val auth: FirebaseAuth
): ViewModel() {

    val signedIn = mutableStateOf(false)
   
    init {
        signedIn.value = currentUser != null
    }
@Composable
fun SignupScreen(navController: NavController,vm:AuthViewModel){

    CheckSignedIn(vm = vm, navController = navController)

}

(I don't write things which are no relation)

When SignupScreen's compose is called, CheckSignedIn's also is called. I don't know in CheckSignedIn how the value of remember { mutableStateOf(false)} is registered and working. My question is, If I finish the app, remember's value is still registered or not?

Every the SignupScreen is called, will the all content of CheckSignedIn's function start from the beginning?

Please tell me how to proceed in the all flow. Thank you.

Upvotes: 2

Views: 949

Answers (1)

Mohamed Rejeb
Mohamed Rejeb

Reputation: 2629

remember helps us to save any state when the app is running or in the background so when you finish the app and start it again everything is going to start from zero, so alreadyLoggedIn state is going to go back to false when you restart the app.

To save states even after closing the app you need to take a look at data store:

documentation: https://developer.android.com/topic/libraries/architecture/datastore

playlist: https://youtube.com/playlist?list=PLWz5rJ2EKKc8to3Ere-ePuco69yBUmQ9C

Upvotes: 2

Related Questions