I am connected to an online database and I have a login form. How to let the user logged in even if he gets out of the application? More details: First, the user will log in into the application and then he will close it. When he gets back to the application I want him to be logged in automatically without asking for username and password even if the phone is not connected to the internet. But when he logs out, it will re-ask him for username and password. Thanks.
Asked
Active
Viewed 208 times
-3
-
2I think there are many different ways to get this done (local db, SharedPreferences etc.). But: please don't store the password as plain text somewhere in your app, use a flag or something else. – kAliert Sep 18 '18 at 14:04
-
Thanks for your reply. Can you give me more details such as examples or tutorial links or videos? – MohamadHassanHijab Sep 18 '18 at 14:05
-
You can find a detailed answer to this on [this question](https://stackoverflow.com/questions/20678669/how-to-maintain-session-in-android). – Rui Casaleiro Sep 18 '18 at 14:10
-
You can do this by saving user id in SharedPreferences. And in onCreate() onResume() method you check if user id is present in SharedPreferences or not. And when user logs out you just remove user id from SharedPreferences – Umer Farooq Sep 18 '18 at 15:01
1 Answers
1
you can use sharedpreferences to check if the user already logged in:
Boolean isLoggedIn = sharedPreferences.getBoolean("Is_LogIn", false);//default value false
if (isLoggedIn){
//redirect user to home page
}else{
//ask for login credentials
}
Alvin Tom
- 119
- 4