Reputation: 14565
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 :
Upvotes: 0
Views: 439
Reputation: 1393
SharedPreference is the best option to access single value in terms of timing which i observe to be true as compared to sql lite.
Upvotes: 1
Reputation: 25755
The problem with storing the state on the device (using either the SharedPreferences
or SQLite) is that the user can manipulate it. So if you simply store "isLoggedIn" in your SQLite-Database, this entry can be manipulated using dpms (for example).
So if you need to store the state, you should use the Application
-class (this can be manipulated, too, but it's harder to do so). If the user closes the application, you destroy the state-variable so he needs to do the login when the application is opened the next time.
For usability, you could store username and (if the user wants so) the password.
Also, if you have a web-service that you use to check if the users login-data is correct, why don't you use for example OAuth to authentify the user on the server and deliver the content from it? This would basically make your application a pure "client".
Upvotes: 1
Reputation: 15269
There can be many reasons when your app looses focus, like pressing home button incoming call, etc but no matter what happens your onPause
method of current Activity
is called so what you have to is-
onPause
method of every activitySharedPreference
to save the state of ApplicationUpvotes: 1
Reputation: 24031
your option SharedPreference
is the perfect one in this condition as you need to store only boolean and you will access if often in your app to change the content.
Upvotes: 1
Reputation: 452
I use static classes or SharedPreference - it'is easy and it's works. SQLite it not good way (in my opinion)
Upvotes: 1