Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep user state in Android

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?

like image 308
rlc Avatar asked Jun 30 '10 15:06

rlc


People also ask

How do I stay logged in to my android?

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.

How can you save the state of activity?

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.

What allows you to properly restore a user state when an activity is restarted?

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.


1 Answers

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");
like image 74
cstack Avatar answered Oct 02 '22 13:10

cstack