Android 如何在活动/应用程序被销毁后保存字符串的值?
我的导航抽屉里有2个Android 如何在活动/应用程序被销毁后保存字符串的值?,android,sharedpreferences,onpause,ondestroy,android-sharedpreferences,Android,Sharedpreferences,Onpause,Ondestroy,Android Sharedpreferences,我的导航抽屉里有2个int,点击应用程序中不同位置的不同按钮,其值会发生变化 我得到了成功增加和更新它们的代码,但问题是,当我关闭或退出应用程序后打开应用程序时,它们被重置 更新后我如何让他们留在那里 如果你想从我的应用程序中获得任何代码,请告诉我 很抱歉,问题格式不正确,但我不知道如何执行此操作,因此我没有发布任何代码。您必须将信息保存在永久性存储器中 您可以使用SharedReferences SharedPreferences prefs= getSharedPreferences("aN
int
,点击应用程序中不同位置的不同按钮,其值会发生变化
我得到了成功增加和更新它们的代码,但问题是,当我关闭或退出应用程序后打开应用程序时,它们被重置
更新后我如何让他们留在那里
如果你想从我的应用程序中获得任何代码,请告诉我
很抱歉,问题格式不正确,但我不知道如何执行此操作,因此我没有发布任何代码。您必须将信息保存在永久性存储器中 您可以使用
SharedReferences
SharedPreferences prefs= getSharedPreferences("aName", MODE_PRIVATE);
//save the value
prefs.edit()
.putInt("nameOfTheValue", theValue).apply();
// get the data
prefs.getInt("nameOfTheValue", aDefaultValue);
您应该将它们另存为onDestroy方法中的
public void onDestroy() {
super.onDestroy();
SharedPreferences settings;
settings = getSharedPreferences("TWO_INT_SAVING", Context.MODE_PRIVATE);
//set the sharedpref
Editor editor = settings.edit();
editor.putInt("FIRST_INT", firstIntValue);
editor.putInt("SECOND_INT", secondIntValue);
editor.commit();
}
然后你可以根据需要把它们拿回来:
SharedPreferences settings;
settings = getSharedPreferences("TWO_INT_SAVING", Context.MODE_PRIVATE);
//get the sharepref
int firstInt = settings.getInt("FIRST_INT", 0);
int secondInt = settings.getInt("SECOND_INT", 0);