简体   繁体   English

共享首选项始终返回默认值

[英]Shared Preference returns always the default value

Here is code I am Using to create and store value in Preference. 这是我用来在Preference中创建和存储值的代码。 outgoing is String variable. outgoing是String变量。

SharedPreferences sp = getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();                
editor.putString("PhoneNo","Hi");
editor.commit();

Here is the code to get value from SharedPreference. 以下是从SharedPreference获取值的代码。

SharedPreferences sp 
=getSharedPreferences(outgoing,Activity.MODE_PRIVATE);
String calln = sp.getString("PhoneNo","0");
Toast.makeText(mContext, "SHARED"+calln,Toast.LENGTH_LONG).show();

You should probably call the getSharedPreferences on the context from which you are accessing them. 您应该在访问它们的上下文中调用getSharedPreferences

Source 资源

Therefore, depending on how you can access your context, if you pass it to some other activity or in an asynchronous task, here are some examples of usage: 因此,根据您如何访问上下文,如果将其传递给其他活动或异步任务,以下是一些用法示例:

this.getSharedPreferences(outgoing, Activity.MODE_PRIVATE);

context.getSharedPreferences(outgoing, Activity.MODE_PRIVATE);

getApplicationContext().getSharedPreferences(outgoing, Activity.MODE_PRIVATE);

Also, one way you can test your stuff is to use a listener when SharedPreferences get changed: 另外,一种可以测试你的东西的方法是在SharedPreferences改变时使用一个监听器:

onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)

Called when a shared preference is changed, added, or removed.

here is how to do that 这是怎么做的

You can also use the Preference Manager to obtain the SharedPreferences : 您还可以使用Preference Manager获取SharedPreferences

PreferenceManager.getSharedPreferences(YOUR_CONTEXT).getString(
                    "PhoneNo", "0");

Or to store them: 或者存储它们:

PreferenceManager.getSharedPreferences(YOUR_CONTEXT).edit().putString(
                    "PhoneNo", "Hi").commit();

改变这种Activity.MODE_PRIVATE这个Activity.MODE_MULTI_PROCESS ,问题是存储价值和访问期间,值可能是由于不同的上下文。

When putting values, try changing this: 在设置值时,请尝试更改此值:

SharedPreferences sp = getSharedPreferences(outgoing, Activity.MODE_PRIVATE);

to this: 对此:

SharedPreferences sp = getApplicationContext().getSharedPreferences(outgoing, Activity.MODE_PRIVATE);

Same when getting values - don't forget to add getApplicationContext() in your call to SharedPreferences 获取值时也一样 - 不要忘记在对SharedPreferences的调用中添加getApplicationContext()

EDIT: Check that your "outgoing" String is the exact same in both Activities 编辑:检查两个活动中的“传出”字符串是否完全相同

The most likely reason that Shared Preference always returns the default value is that you saved the value in one preference file and then tried to retrieve it in another preference file. 共享首选项始终返回默认值的最可能原因是您将值保存在一个首选项文件中,然后尝试在另一个首选项文件中检索它。 This can happen if you do call getPreferences() from different Activities, because getPreferences() creates a different preference file based on the activity it is created in. 如果您从不同的活动中调用getPreferences() ,则会发生这种情况,因为getPreferences()会根据创建的活动创建不同的首选项文件。

Solution

The easiest solution is to always get your shared preferences like this: 最简单的解决方案是始终获得这样的共享首选项:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);

This will use a single preference file for the entire app. 这将为整个应用程序使用单个首选项文件。

Alternate solution 替代解决方案

If for some reason you need to use different preference files, then you can do 如果由于某种原因您需要使用不同的首选项文件,那么您可以这样做

final static String PREF_FILE_1 = "pref_file_1";
...
SharedPreferences sharedPref = context.getSharedPreferences(PREF_FILE_1, Context.MODE_PRIVATE);

Just make sure you always use the right file name for the preferences you are trying to save and retrieve. 只需确保始终使用正确的文件名作为您要保存和检索的首选项。

Local preferences 本地偏好

If you really only need a preference for a specific Activity, then you can use getPreferences(Context.MODE_PRIVATE) . 如果您确实只需要特定Activity的首选项,则可以使用getPreferences(Context.MODE_PRIVATE) Just don't expect to be able to retrieve the values from another Activity in the same way. 只是不要期望能够以相同的方式从另一个Activity中检索值。

See also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM