Home » Android » Android: SharedPreference: Defaults not set at startup

Android: SharedPreference: Defaults not set at startup

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have Listpreferences in my app. They don’t appear to be setting to their defaults right after installation – they appear to be null. I’m trying to figure out why my default preferences are not being set right after installation. In my main code I have:

      SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

      InUnits = sp.getString("List1", "defValue");
       InAngs = sp.getString("List2", "defValue");
       OutUnits = sp.getString("List3", "defValue");
       OutAngs = sp.getString("List4", "defValue");

Right after the above code executes, each variable contains “defValue” instead of the actual values I have assigned in my ListPreference below.

My preference xml file is called, “settings.xml”. Here’s what one of the ListPreferences there looks like:

       <ListPreference
       android:key="List1"
       android:title="Input: Alph"
       android:summary="Choose Alph or Ralph"
       android:entries="@array/inputAlph"
       android:entryValues="@array/input_Alph_codes"
       android:dialogTitle="Input Alph"
       android:defaultValue="ININ"/>           

Here’s what some of my strings.xml file looks like:

<string-array name="inputUnits">
    <item>Alph</item>
    <item>Ralph</item>  
    </string-array>   
    <string-array name="input_Alph_codes">
    <item>ININ</item>
    <item>INMM</item>
    </string-array>

When I go to menu, and then settings, I can see my defaults checked (radiobuttoned). Then when I go back from the settings menu to my main screen – all is well – for life! …then each var above is assigned the proper default value.

This only happens when I first install my app on the phone. After I go to the settings screen once and then right out of it, the app is fine and accepts any setting changes.

By the way, as you can see, “List1” is the android:key within a file called settings.xml in my res/xml folder.

How to&Answers:

They don’t appear to be setting to
their defaults right after
installation – they appear to be null.

That’s what’s supposed to happen.

I’m trying to figure out why my
default preferences are not being set
right after installation.

They’re not supposed to be. The preference XML you have listed there is only used for populating a PreferenceActivity, nothing more. Until the user opens the PreferenceActivity, the preferences will be null, and the defaults you supply to the SharedPreferences getters will be returned.


UPDATE

You can use setDefaultValues() on PreferenceManager to assign the defaults from your preference XML to a SharedPreferences. However, be careful of the timing — this will do disk I/O, and therefore ideally is performed on a background thread.

Answer:

Set the default values to SharedPreferences from your preference XML.

PreferenceManager.setDefaultValues(Context context, int resourceId, boolean readAgain)

PreferenceManager.setDefaultValues

Answer:

You can specify a default value like this

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.getString("thePrefKey", "theDefaultValue");

The android:defaultValue=”…” in the “layout” settings.xml is only a visual help for user