开发者

defaultValue from xml preference file isn't stored - why?

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="@开发者_如何学Cstring/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>    


Depending on what the superclass of your com.myapp.TimePreference is, you may have to persist the default value yourself in onSetInitialValue(). EditTextPreference has implemented this, but DialogPrefercence or Preference only has an empty implementation.

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


You have to explicitly apply defaults. Let's assume you have preferences.xml file, then you have to call:

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

You can do this from you main activity or (a better approach) from your Application class (in onCreate method). For more info about later approach see Application documation and android:name attribute documentation in application tag in AndroidManifest.xml

Note: Default values from preference.xml will also be applied when user opens PreferenceActivity for the first time. Of cause this PreferenceActivity has to populate preferences using preference.xml.


I've found a solution to my problem, but it still doesn't answer my question. I had to change the line:

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

into:

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

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

"Note: this will NOT reset preferences back to their default 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!


I have exactly the same problem with simple integers defaults. Nor true, nor false in the setDefaultValues() can populate some of the new preferences with their defaults, even after the preferences activity opened. I have added these lately to the xml file. They are starting to work only after editor.Edit() procedures. I'm building for 2.1, by the way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜