Search code examples
androidauthenticationandroid-jetpack-composeandroid-roomandroid-jetpack

How can I initialize a screen to check if a user is logged in using room database after closing an application?


So this is my logic. I will have an authenticate function in my loginViewModel which basically retrieves a user from the database and checks if it matches the input from a textField. I will have another function to log the user out from a different screen, it will cut off its connection with the database.

While that logic is clear in my mind, I don't have any idea to persist the state if a user is already logged in or not after closing the application. So when opening the app, if the user is already logged in, they should be navigating to the homescreen immediately. Is there any way to achieve this??


Solution

  • You will need a key value boolean saved in something like data store preferences it is useful for a case like this and you change it to true when user logs in and to false when he logs out . And then you will need check at the opening of the app if that value is true you direct him to home screen otherwise user goes to login screen.