I am struggling to restore the default values I specified in the preferences.xml, Here is my code:
Preference reset = findPreference(res.getString(R.string.DEFAULT_PREFS));
reset.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference p) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Preferences.this);
sp.edit().clear().commit();
PreferenceManager.setDefaultValues(Preferences.this, R.layout.preferences, true);
return true;
}
});
This code is my understanding of the android’s developers reference for the function setDefaultValues(context, resId, readAgain):
Parameters
context The context of the shared preferences.
resId The resource ID of the preference hierarchy XML file.
readAgain Whether to re-read the default values.Note: this will NOT reset preferences back to their default values.
For that functionality, use getDefaultSharedPreferences(Context)
and clear it followed by a call to this method with
this parameter set to true.
Well, it does not work, the preferences values are the same after this code is executed.
Then I looked into the SharedPreferences variable sp, and it points to a system generated file in the path:
/data/data/<packagename>/shared_prefs/<packagename>_preferences.xml
which I can only assume is the same xml I provided when I created the activity.
addPreferencesFromResource(R.layout.preferences);
Also inspecting the sp variable, the hash table has all the preferences, but there is no field for default value.
EDIT:
Before I am asked to, here is an excerpt from the preferences.xml file
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:defaultValue="5000"
android:key="@string/MAX_MESSAGES"
android:numeric="integer"
android:summary="@string/MAX_MESSAGES_desc"
android:title="@string/MAX_MESSAGES_title" />
<EditTextPreference
android:defaultValue="10"
android:key="@string/VIEW_EDGE_ROWS"
android:numeric="integer"
android:summary="@string/VIEW_EDGE_ROWS_desc"
android:title="@string/VIEW_EDGE_ROWS_title" />
<ListPreference
android:defaultValue="0"
android:entries="@array/level_list"
android:entryValues="@array/level_values"
android:key="@string/INITIAL_ORG"
android:summary="@string/INITIAL_ORG_desc"
android:title="@string/INITIAL_ORG_title" />
<ListPreference
android:defaultValue="2"
android:entries="@array/view_list"
android:entryValues="@array/view_values"
android:key="@string/INITIAL_VIEW"
android:summary="@string/INITIAL_VIEW_desc"
android:title="@string/INITIAL_VIEW_title" />
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/AUTOSCROLL"
android:summary="@string/AUTOSCROLL_desc"
android:title="@string/AUTOSCROLL_title" />
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/SEND_THEN_EXIT"
android:summary="@string/SEND_THEN_EXIT_desc"
android:title="@string/SEND_THEN_EXIT_title" />
<Preference
android:key="@string/DEFAULT_PREFS"
android:summary="@string/DEFAULT_PREFS_desc"
android:title="@string/DEFAULT_PREFS_title" />
</PreferenceScreen>
This is my solution so far. After debugging into the Android source code I find out that
setDefaultValues()is not reliable and does not work as intended (at least according to my expectations).I restore the default values manually now. I have a map where I can fetch from the default values.
Here is an interesting note: Inspecting the Preference class shows it has a field called
mDefaultValuewhich contains the default value for the preference. But this field can only be set bysetDefaultValue()method, and there is no method to get it. It would have save me the need for a MapThis is the code I use now, tested and working:
Another note:
editor.commit()updates the preferences file, but does not update the preferences screen. You have to update each preference by using the listener (OnSharedPreferenceChangeListener()).