3

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>    

cody
  • 6,389
  • 15
  • 52
  • 77

4 Answers4

5

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));
}
Wei Liu
  • 555
  • 8
  • 24
4

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.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • Hi, thanks for your answer! Unfortunately that doesn't seem to work. :-( I did a full uninstall, put the line in my app entry activity and looked into the sharedprefs-file after starting again. I still find the defaults for every ListPreference that can be found in the preferences.xml, but there are no defaults for CheckBoxPreference and TimePreference in the file. Do you know why? – cody May 31 '11 at 00:13
  • You should add your `preferences.xml` to the question. If its too long, try reducing it to 4-5 preferences. – inazaruk May 31 '11 at 05:51
  • I attached the code, I think it's nothing really exciting. But it doesn't seem to work :( – cody May 31 '11 at 17:46
  • While this is the cause in some cases, it appears that in this case the issue is identified in Wei's answer. – nmr Aug 14 '15 at 16:33
1

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!

cody
  • 6,389
  • 15
  • 52
  • 77
  • Someone probably updated docs, bacause from setDefaultValues(...) docs, right after "Note: this will NOT reset preferences back to their default values." one can find the following "For that functionality, use PreferenceManager.getDefaultSharedPreferences(Context) and clear it followed by a call to this method with this parameter set to true." – Kresimir Aug 23 '15 at 22:55
0

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.

halxinate
  • 1,509
  • 15
  • 22