save variables after quitting application?

独自空忆成欢 提交于 2019-11-28 19:28:33

You should be using SharedPrefences. They are quite simple to use and will store the variables in the application data. As long as the user never hits "Clear Data" in the settings for your application, they will always be there.

Here is a code sample.

To access variables:

SharedPreferences mPrefs = getSharedPreferences("label", 0);
String mString = mPrefs.getString("tag", "default_value_if_variable_not_found");

To edit the variables and commit (store) them:

SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.putString("tag", value_of_variable).commit();

Make sure both "tag" fields match!

MAC

Use SharedPreference, this is a better option.

To see this tutorial.

sample code: save:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = settings.edit();
editor.putString("statepara1", ts);
editor.commit();

get:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);    
String ret = settings.getString("statepara1", "0");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!