I’m asking this question because I want to be sure which is the best way to store the user “logged in” / “logged out” state in my android application.
Here is what I am doing. In my application I connect to the server, send him username and password and if everything is ok it returns me OK. So now I want to be able to save the user logged in state all over my application because I need to change the content depends on that if user is logged in or not.
And the other thing is that I need to be able to save the user’s login state, so the next time when he start the application he will be automatically logged in.
Any suggestions or ideas which is the best way to do that?
I made a little research over internet and find a few ways to do that :
- MyApplication class which extends android.app.Application
- SharedPreference
- Or maybe a SQLite
There can be many reasons when your app looses focus, like pressing home button incoming call, etc but no matter what happens your
onPausemethod of currentActivityis called so what you have to is-onPausemethod of every activitySharedPreferenceto save the state of Application