在共享preferences所有活动访问存储数据 [英] Storing data in SharedPreferences accessible to all activities
问题描述
我要存储和检索数据,对于使用共享preferences在我的应用程序的所有活动进行访问。那可能吗?到现在为止,我已经做了这样的数据存储为特定的活动。
I want to store and retrieve data that is accessible to all activities in my app using SharedPreferences. Is that possible? Up until now I have been doing it such that the data is stored for a particular activity.
推荐答案
是的。分享preferences做的正是这一点。
在每一个活动,你可以这样:
Yes. SharePreferences do exactly this. In every activity you can this:
SharedPreferences prefs = getSharedPreferences(ApplicationConstants.PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(name, value);
editor.commit();
,然后检索其他activty这样做值:
And then retrieve values in other activty doing this:
mPrefs.getString(name, "");
这是文档:
的http://developer.android.com/reference/android/content/Shared$p$pferences.html
这是一个很好的例子入手:
And this is a good example to start with:
的http://myandroidsolutions.blogspot.it/2012/03/android-$p$pferenceactivity.html
这篇关于在共享preferences所有活动访问存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!