Search code examples
androidsharedpreferencesandroid-preferencesandroid-sharedpreferences

Why is the defaultValue of my preference not being saved to SharedPreferences?


In the following SSCCE, the idea is that there is a button in the main activity, and when the user clicks it, (the method showCheckBoxPreferencesValue() is invoked) the default value of the only preference defined in preferences.xml (using android:default_value) should be displayed in the textbox below this button.

In order to get this default value to display in the textbox, I use the method sharedPreferences.getString() and pass Preference does not exist as its second argument, which means if the preference (with the key passed as first argument) does not seem to exist, then the string Preference does not exist is set to the textbox. (Source)

And that IS happening? What am I doing wrong? See screenshot at the end.

res/xml/preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <EditTextPreference android:key="@string/preferences_editTextPreference_key"
        android:title="@+id/preferences_editTextPreference_title"
        android:defaultValue="@string/preferences_editTextPreference_defaultValue" />

</PreferenceScreen>

MainActivity.java:

public class MainActivity extends Activity {
    private String EDIT_TEXT_PREFERENCE_KEY;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.mainActivity_textView);

        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

    }

    public void showCheckBoxPreferencesValue(View view) {

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        EDIT_TEXT_PREFERENCE_KEY = getResources().getString(R.string.preferences_editTextPreference_key);

        String editTextPreferenceValue = sharedPreferences.getString(EDIT_TEXT_PREFERENCE_KEY,
                "Preference does not exist");

        textView.setText("Checkbox Preference Value: " + editTextPreferenceValue);
    }
}

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Preferences Practice</string>
    <string name="mainActivity_button">Show EditText Preference\'s Value</string>
    <string name="preferences_editTextPreference_key">EditTextPreferenceKey</string>
    <string name="preferences_editTextPreference_title">EditTextPreference Title</string>


    <string name="preferences_editTextPreference_defaultValue">EditTextPreference Default Value String</string>

</resources>

enter image description here


Solution

  • I tried creating a new app from the code you supplied.

    I had to recreate your layout file, and I did:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="my.package.test.MainActivity">
    
        <TextView
            android:id="@+id/mainActivity_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
        <Button
            android:layout_below="@id/mainActivity_textView"
            android:onClick="showCheckBoxPreferencesValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CLICKME"
            />
    </RelativeLayout>
    

    (Next time please include it =)! )

    I ran the code and it looks like this:

    enter image description here

    So as you can see, it works.

    I think your problem might be that you ran your code more than once, without the value key correctly spelled the first time (or some other misfortune). You see, you choose to send false as the last parameter to setDefaultValues, which forces it to only read the default values once (the first time the app is ever launched). Try uninstalling your app and reinstalling it, or set the flag to true.

    That is:

    PreferenceManager.setDefaultValues(this, R.xml.preferences, true);