sharedPreferences中的boolean并不总是正确保存

时间:2017-02-10 09:02:18

标签: android boolean android-sharedpreferences

我有一个奇怪的问题。在一个活动中,我有复选框字段的列表,复选框的状态取决于共享的优先级,但它的工作有时,有时不工作(这意味着有时当我选中复选框然后回到上一个活动,然后返回我的checkBox活动,sharedPrefrences值没有改变)。我认为它发生了,因为我太快地改变了活动,所以我把保存逻辑放在线程中但遇到了同样的问题。

以下是我尝试过的一些代码:

    public final static String PREFERENCES_NAME = "USER_PREFERENCES";
       public final static String PLAY_REQUIRED = "PLAY_REQUIRED";
          sharedPreferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
        recordCheckBox=(CheckBox)findViewById(R.id.recordRequiredCheckBox);
        recordCheckBox.setChecked(sharedPreferences.getBoolean(PLAY_REQUIRED,true));
        findViewById(R.id.playRequiredItem).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
SharedPreferences.Editor preferencesEditor = sharedPreferences.edit();
                if(recordCheckBox.isChecked()) {
                    recordCheckBox.setChecked(false);
                    preferencesEditor.putBoolean(PLAY_REQUIRED, false).apply();
                    preferencesEditor.commit();
                    Log.i("kamil","AfterSetFalseCheckBox :: "+ sharedPreferences.getBoolean(PLAY_REQUIRED,true));
                }
                else {
                    recordCheckBox.setChecked(true);
                    preferencesEditor.putBoolean(PLAY_REQUIRED, true).apply();
                    preferencesEditor.commit();
                    Log.i("kamil","AfterSetTrueCheckBox :: "+ sharedPreferences.getBoolean(PLAY_REQUIRED,true));
                }
            }
        });

2 个答案:

答案 0 :(得分:1)

我认为这是因为每当你将值放在sharedpreference中而不是在一个sharedprefrences中插入的值时,以及当你从sharedpreference获取值来自另一个共享首选项时。因此,我建议您为共享首选项创建适当的帮助程序类,并在需要时使用它。

这是一个例子:

 public class PreferenceHelper {

    public static SharedPreferences AppPreference;

    // Preference name for app.
    public static final String APP_PREFERENCE_NAME = "app_preference_name";

     /**
     * Set boolean value to shared-preference.
     *
     * @param key   Key for store boolean value to shared-preference.
     * @param value Boolean value to be stored in shared-preference for given key.
     */
    public static void putBoolean(Context context,String key, boolean value) {
        AppPreference = context
                .getSharedPreferences(APP_PREFERENCE_NAME, Context.MODE_PRIVATE);
        Editor editor = AppPreference.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * Get boolean value from shared-preference.
     *
     * @param key          Key for getting boolean value from shared-preference.
     * @param defaultValue Default boolean value that is returned if given key is not found in
     *                     preference.
     * @return string      Boolean value from shared-preference for given key.
     */
    public static boolean getBoolean(Context context,String key, boolean defaultValue) {
        AppPreference = context
                .getSharedPreferences(APP_PREFERENCE_NAME, Context.MODE_PRIVATE);
        boolean string = AppPreference.getBoolean(key, defaultValue);
        return string;
    }
}

并使用它:

设定值:

PreferenceHelper.putBoolean(getApplicationContext(),"key",value);

获得价值:

Boolean boolean = PreferenceHelper.getBoolean(getApplicationContext(),"key",defalut-value);

答案 1 :(得分:-1)

您可以使用以下方法设置和获取布尔值。

/ 用于在首选项中设置布尔值 /

public void setBooleanValue(String key, boolean value, Context context) {

    SharedPreferences preferences = context.getSharedPreferences(
            context.getPackageName(), Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

/ 用于获取首选项中的布尔值 /

public boolean getBooleanValue(String key, Context context) {

    SharedPreferences preferences = context.getSharedPreferences(
            context.getPackageName(), Context.MODE_PRIVATE);
    boolean value = preferences.getBoolean(key, false);
    return value;
}