Android sharedPreferences 不会在活动之间共享
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11747687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sharedPreferences won't share between activities
提问by user922220
I'm trying to use SharedPreferences to save settings. But I can't seem to get the data to be shared between any of my activities. The code I'm using does manage to save settings but each activity seems to have it's own version of each variable.
我正在尝试使用 SharedPreferences 来保存设置。但我似乎无法在我的任何活动之间共享数据。我使用的代码确实设法保存设置,但每个活动似乎都有自己的每个变量版本。
So for example. I have an audio settings activity in which the user can give a value to a variable "musicVolume" which is saved. If I close the game and reload it, then the audio settings activity "remembers" the value. But if I try to load the value into any other activity it doesn't work. But, they can all load and save their own variables of the same name.
所以例如。我有一个音频设置活动,用户可以在其中为保存的变量“musicVolume”赋值。如果我关闭游戏并重新加载它,则音频设置活动会“记住”该值。但是,如果我尝试将该值加载到任何其他活动中,则它不起作用。但是,它们都可以加载和保存自己的同名变量。
These are the methods I'm using to save the variables. There is a copy of each of these methods in each activity.**
这些是我用来保存变量的方法。每个活动中都有这些方法的副本。**
As I say, they work, but they can only seem to read and write data for the individual activity in which they are located.
正如我所说,它们可以工作,但它们似乎只能为它们所在的单个活动读取和写入数据。
public void SavePreferences(String key, float value) {
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putFloat(key, value);
editor.commit();
}
public void LoadPreferences() {
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
musicVolume = sharedPreferences.getFloat("musicVolume", (float)0.123);
soundEffectsVolume = sharedPreferences
.getFloat("soundEffectsVolume", (float)0.123);
}
public void ClearPreferences() {
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
}
** I know there is a better way to do this but I'm a very novice oo programmer. I tried to follow the advice of another thread
** 我知道有更好的方法可以做到这一点,但我是一个非常新手的 oo 程序员。我试着听从另一个线程的建议
but wherever I tried to put the lines
但无论我在哪里尝试放置线条
protected AppPreferences appPrefs;
appPrefs = new AppPreferences(getApplicationContext());
I get an error of one sort or another. But most importantly, reading the other comments on the thread, people are saying that SharedPreferences are automatically shared between activities within the same package anyway, which is how I thought they work.
我收到了一种或另一种错误。但最重要的是,阅读线程上的其他评论,人们说 SharedPreferences 无论如何都会在同一个包中的活动之间自动共享,这就是我认为它们的工作方式。
回答by Niraj Burde
You are using getPreferences(MODE). Use getSharedPreferences("PREF_NAME", MODE)instead. This way you will provide a name to particular preference and then you can call it by its name (PREF_NAME here) from whatever the activity you want.
您正在使用getPreferences(MODE)。请改用getSharedPreferences("PREF_NAME", MODE)。通过这种方式,您将为特定偏好提供一个名称,然后您可以在任何您想要的活动中按其名称(此处为 PREF_NAME)调用它。
//------get sharedPreferences
SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
//-------get a value from them
pref.getString("NAME", "Android");
//--------modify the value
pref.edit().putString("NAME", "Simone").commit();
//--------reset preferences
pref.edit().clear().commit();
回答by Matt Shaw
As was said, use getSharedPreferences(String name, int mode)
rather than getPreferences (int mode)
. Specifically, if you are interested, the documentation for these two methods illustrates the difference. According to the Android documentationgetPreferences(int)
does the following:
如前所述,使用getSharedPreferences(String name, int mode)
而不是getPreferences (int mode)
. 具体来说,如果您有兴趣,这两种方法的文档说明了区别。根据Android 文档getPreferences(int)
执行以下操作:
Retrieve a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.
检索 SharedPreferences 对象以访问此活动专用的首选项。这只是通过传入此活动的类名作为首选项名称来调用底层 getSharedPreferences(String, int) 方法。
回答by Frank Sposaro
If you aren't doing anything fancy with preferences I would just use the default way of accessing them. It seems to be your problem.
如果您没有对偏好做任何花哨的事情,我只会使用默认的访问方式。看来是你的问题。
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Here is a great write with more detail, put you are doing everything pretty much correctly except for the getting your handle. How do I get the SharedPreferences from a PreferenceActivity in Android?
这是一篇包含更多细节的精彩文章,除了掌握句柄外,您几乎可以正确地做所有事情。 如何从 Android 中的 PreferenceActivity 获取 SharedPreferences?
ALSO: Don't forget the new way is to call .apply() instead of .commit() This was in one on the #io2012 videos..
另外:不要忘记新方法是调用 .apply() 而不是 .commit() 这是#io2012视频中的一个。
回答by Singhak
You should use this
你应该用这个
SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
Or
或者
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
回答by Yuri Cavalcante
If you had the same problem as me, it's very simple. When you to go save the preferences, save it like this:
如果你和我有同样的问题,那就很简单了。当您要保存首选项时,请像这样保存:
SharedPreferences sp = getSharedPrefenreces("Preference",MODE_PRIVATE);
And not:
并不是:
SharedPreferences sp = getSharedPrefenreces("Preference's name",Context.MODE_PRIVATE);
I know how so much is important the SharedPrefenrences in some cases.
我知道在某些情况下 SharedPrefences 有多么重要。