SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putInt(KEY_NAME, THE_INTEGER_VALUE);
// Replace `putInt` with `putString` if your value is a String and not an Integer.
editor.commit();
SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
int counter = sharedPrefs.getInt(KEY_NAME, 0);
// Replace the `int counter` with `String counter` if your value is a String and not an Integer.
// Also, replace the `getInt` with `getString`
# 1 楼答案
您可以这样做:
在
onPause()
中,使用此代码将计数器的值保存到SharedReference文件PREFERENCE_FILE_NAME
,以选择将创建用于存储值的XML文件李>THE_INTEGER_VALUE
是实际值李>在
onResume()
中,您可以检索并显示该值:您可以稍后使用
int counter
在TextView
中显示# 2 楼答案
使用共享优先权作为doc example读/写int值的更改:
# 3 楼答案
您需要以这种方式使用
SharedPreferences
:保存值,然后再次获取它然后在其他
Activity
中还原SharedPreferences
:# 4 楼答案
您可以使用SharedPreferences来实现您想要的。设置
count
,退出应用程序时,重新打开应用程序时,从那里获取应用程序例如,关于如何使用它,请选中此out