When the application is first startet, I’d like to store all default values I’ve defined in my prefences.xml by using the ‘android:defaultValue’ attribute, but some of them are not stored on the device – can someone tell me why?
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="@string/prefs_cat_title_x">
<ListPreference
android:key="@string/prefs_key_1"
android:title="@string/prefs_title_1"
android:summary="@string/prefs_summary_1"
android:entries="@array/array1"
android:entryValues="@array/array1"
android:defaultValue="@string/prefs_default_1"/>
<com.myapp.TimePreference
android:key="@string/prefs_key_2"
android:title="@string/prefs_title_2"
android:defaultValue="@string/prefs_default_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.myapp.TimePreference
android:key="@string/prefs_key_3"
android:title="@string/prefs_title_3"
android:defaultValue="@string/prefs_default_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListPreference
android:key="@string/prefs_key_4"
android:title="@string/prefs_title_4"
android:summary="@string/prefs_summary_4"
android:entries="@array/array2"
android:entryValues="@array/array2"
android:defaultValue="@string/prefs_default_4"/>
<CheckBoxPreference
android:key="@string/prefs_key_5"
android:title="@string/prefs_title_5"
android:summary="@string/prefs_summary_5"
android:defaultValue="false"/>
<CheckBoxPreference
android:key="@string/prefs_key_6"
android:title="@string/prefs_title_6"
android:summary="@string/prefs_summary_6"
android:defaultValue="false"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/prefs_cat_title_common">
<com.myapp.DatabaseResetPreference
android:title="@string/prefs_title_7"
android:summary="@string/prefs_summary_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</PreferenceCategory>
I’ve found a solution to my problem, but it still doesn’t answer my question.
I had to change the line:
into:
As the docs say, setting readAgain should not overwrite any existing preference values:
Simply using “true” works for me, but I still don’t know why only the defaults for three of my preferences are set when using “false”, even though the xml file containing KEY_HAS_SET_DEFAULT_VALUES didn’t exist (and so wasn’t set to true) on the device (it existed not until I called the method above).
If anyone knows a possible reason for that behavior, please let me know!