Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在应用程序关闭并重新启动后记住应用程序的数据_Android - Fatal编程技术网

Android 在应用程序关闭并重新启动后记住应用程序的数据

Android 在应用程序关闭并重新启动后记住应用程序的数据,android,Android,如何在应用程序关闭后记住android应用程序上的数据?例如:在android calculator中,它会记住以前计算的数据,在应用程序重新启动后,您可以从SharedPreferences中获益 更多信息请点击此处: 我给你举个简单的例子 假设我们要存储一个字符串结果 我们将其保存在SharedPref中,如下所示: PreferenceManager.getDefaultSharedPreferences(this).edit().putString("Result","your valu

如何在应用程序关闭后记住android应用程序上的数据?例如:在android calculator中,它会记住以前计算的数据,在应用程序重新启动后,您可以从SharedPreferences中获益 更多信息请点击此处:

我给你举个简单的例子

假设我们要存储一个字符串结果

我们将其保存在SharedPref中,如下所示:

PreferenceManager.getDefaultSharedPreferences(this).edit().putString("Result","your value").apply();
String result = PreferenceManager.getDefaultSharedPreferences(this).getString("Result","defaultValue");
下次在应用程序内重新启动或启动时,我们会这样做:

PreferenceManager.getDefaultSharedPreferences(this).edit().putString("Result","your value").apply();
String result = PreferenceManager.getDefaultSharedPreferences(this).getString("Result","defaultValue");
默认值为:如果此首选项不存在,则返回的值

您可以在中使用getActivity或getApplicationContext更改此设置 一些案例

当然,您可以使用putInteger或putBoolean更改putString和getString,并使用getInteger或getfloat等获得它们