I am developing an Android app that needs to receive specific informations for each user.
I made an authentication using GET and POST methods. Now I have the cookie delivered by the server when the username and password are correct.
How do I store this data? I looked for but couldn't find the best way to store a session in Android.
How applications like Foursquare, Facebook for example keep the state of an user? How do they persist data?
When users log in to your application, store the login status into sharedPreference and clear sharedPreference when users log out. Check every time when the user enters into the application if user status from shared Preference is true then no need to log in again otherwise direct to the login page.
When the activity goes into the background, the system calls onSaveInstanceState() . You should save the search query in the onSaveInstanceState() bundle. This small amount of data is easy to save. It's also all the information you need to get the activity back into its current state.
onSaveInstanceState() is called by Android if the Activity is being stopped and may be killed before it is resumed! This means it should store any state necessary to re-initialize to the same condition when the Activity is restarted.
If you just want to store some user info, you can use SharedPreferences as long as you can get the text from the server.
Once you have the text you want to save:
final SharedPreferences prefs = context.getSharedPreferences();
Editor editor = prefs.edit();
editor.putString("field_name", data);
editor.commit();
Once you copy the info from the server to the SharedPreferences, you can access it like so:
data = prefs.getString("field_name");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With