DefaultValue from xml preferences file is not saved - why?

When the application is the first startet, I would like to keep all the default values ​​that I defined in my prefences.xml using the android: defaultValue attribute, but some of them are not stored on the device. did anyone 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>    

+3
source share
4 answers

I found a solution to my problem, but it still does not answer my question. I had to change the line:

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

at

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

As the docs say, the readAgain installation should not overwrite any existing preference values:

"Note: this does NOT reset preferences to revert to default values."

"" , , "false", XML , KEY_HAS_SET_DEFAULT_VALUES, ( true) ( , ).

- , , !

+1

, com.myapp.TimePreference, onSetInitialValue(). EditTextPreference , DialogPrefercence Preference .

protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    persistString(restoreValue ? 
        getPersistedString((String)defaultValue) : (String)defaultValue));
}
+5

. , preferences.xml, :

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

( ) Application ( onCreate). . Application documation android:name Application AndroidManifest.xml

. preference.xml , PreferenceActivity. - PreferenceActivity preference.xml.

+4

I have exactly the same problem with simple integer defaults. Both true and false in setDefaultValues ​​() can fill some of the new settings with their default values, even after the settings are open. I have added them recently in an xml file. They only start working after the editor.Edit () routines. By the way, I'm building for 2.1.

0
source

All Articles