开发者

Keep user state in Android

开发者 https://www.devze.com 2023-01-05 10:29 出处:网络
I am developing an Android app that needs to receive specific informationsfor each user. I made an authentication using GET and POST methods. Now I have the cookie delivered by the server when the us

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开发者_StackOverflow中文版 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?


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");


Those apps most likely use the AccountManager to store account data. In your case it may be easier for you to just store the user's login information into SharedPreferences that you can recall when the Activity starts.

0

精彩评论

暂无评论...
验证码 换一张
取 消