Chris
Chris

Reputation: 210

SharedPreferences not saving / persisting

I don't understand why my checkbox preference is not saved or not read properly. I have the following code in AndroidManifest.xml:

<activity android:name=".Preferences" />

And then in res/xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
<PreferenceCategory 
    android:title="Preferences">
   <CheckBoxPreference 
       android:title="Always Run"
       android:key="@string/pref_always_run_key" 
       android:summary="Always run it" />
</PreferenceCategory>

And in res/values/strings.xml

<string name="pref_always_run_key">always_run_default</string>

And then I have a src/com.name/Preferences.java file

public class Preferences extends PreferenceActivity
{
private static final String LOG_TAG = Preferences.class.getSimpleName() + "_LOG";

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences); //5

    //getPreferenceManager().setSharedPreferencesName();

    CheckBoxPreference alwaysRunCheckBox = (CheckBoxPreference)findPreference(getString(R.string.pref_always_run_key));// 8

    alwaysRunCheckBox.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
    {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue)
        {

            Log.d(LOG_TAG, "User set always run to " + newValue.toString().equals("true"));
            return true;
        }
    });
}
}

And then I have in the MainActivity.java in a test button I have

SharedPreferences prefs = MainActivity.this.getSharedPreferences("com.projname.Preferences", Context.MODE_PRIVATE);
            String defaultAlwaysRunKey = MainActivity.this.getResources().getString(R.string.pref_always_run_key);
            Log.d(LOG_TAG, "Always run key is " + defaultAlwaysRunKey);
            boolean run = prefs.getBoolean(defaultAlwaysRunKey, false);
                            Log.d(LOG_TAG, "It contains the key: " + prefs.contains(defaultAlwaysRunKey));
            Log.d(LOG_TAG, "Always run set to " + run);
Log.d(LOG_TAG, "All the preferences saved are: " + prefs.getAll().toString());

And the output is always:

10-25 16:15:50.844: D/MainActivity_LOG(1219): Clicked on test...
10-25 16:15:50.844: D/MainActivity_LOG(1219): Always run key is always_run_default
10-25 16:15:50.844: D/MainActivity_LOG(1219): It contains the key: false
10-25 16:15:50.844: D/MainActivity_LOG(1219): Always run set to false
10-25 16:15:50.855: D/MainActivity_LOG(1219): All the preferences saved are: {}

regardless of whether the checkbox is checked or not.

Upvotes: 3

Views: 3985

Answers (2)

yoah
yoah

Reputation: 7230

I think your issue is that you read from a different preferences file than the one you write to. You read by name, but write to the default.

Try to change the MainActivity assignment to

SharedPreferences prefs =
   PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

Upvotes: 4

t0mm13b
t0mm13b

Reputation: 34592

You have forgotten to use the SharedPreference's editor and call the commit(); method of the editor object.

CheckBoxPreference alwaysRunCheckBox = (CheckBoxPreference)findPreference(getString(R.string.pref_always_run_key));// 8

    alwaysRunCheckBox.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
    {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue)
        {

            boolean val = Boolean.parseBoolean(newValue.toString());

            Editor e = preference.getEditor();
            e.putBoolean(getString(R.string.pref_always_run_key), val );
            e.commit();
            Log.d(LOG_TAG, "User set always run to " + String.valueOf(val));

            return true;

        }
    });

Upvotes: 4

Related Questions