Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7984459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:16:30+00:00 2026-06-04T11:16:30+00:00

I am struggling to restore the default values I specified in the preferences.xml, Here

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:16:32+00:00Added an answer on June 4, 2026 at 11:16 am

    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 mDefaultValue which contains the default value for the preference. But this field can only be set by setDefaultValue() method, and there is no method to get it. It would have save me the need for a Map

    This is the code I use now, tested and working:

        Preference reset = findPreference(getResources().getString(R.string.DEFAULT_PREFS));
        reset.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
    
            public boolean onPreferenceClick(Preference p) {
                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Preferences.this);
                SharedPreferences.Editor editor = sp.edit();
                editor.clear();
                // PreferenceManager.setDefaultValues(Preferences.this, R.layout.preferences, false);
                for(int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
                    restoreDefault(editor, getPreferenceScreen().getPreference(i));
                }
                editor.commit();
                return true;
            }
        }
    
    private void restoreDefault(Editor editor, Preference p) {
        if(p instanceof PreferenceCategory) {
            PreferenceCategory pCat = (PreferenceCategory) p;
            for(int i = 0; i < pCat.getPreferenceCount(); i++) {
                restoreDefault(editor, pCat.getPreference(i));
            }
        }
        if(p instanceof ListPreference) {
            editor.putString(p.getKey(), resMap.get(p.getKey())._default);
        }
        if(p instanceof EditTextPreference) {
            editor.putString(p.getKey(), resMap.get(p.getKey())._default);
        }
        if(p instanceof CheckBoxPreference) {
            editor.putBoolean(p.getKey(), resMap.get(p.getKey())._default.equals("true"));
        }
    }
    

    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()).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Struggling a bit here... My view controller adheres to following protocols In my init
im struggling with my cake code(very new to cakephp) and was after some help.
im struggling with syntax here: hopefully this question is v simple, im just miising
Struggling with this tournament fixtures algorithm. The code is working perfectly but I need
Struggling with onchange for JQuery Datetime plugin: http://plugins.jquery.com/project/datetime I am using the following code.
still struggling with regex :) i have this code: $link = '/\bhttp:\/\/.+\..+[\/\w]?\b/i'; $match =
Struggling to learn Haskell, how does one take the head of a string and
Struggling with the basics here. I want to setup an array, and fill it
Struggling here... I am trying to have views displayed during specific periods in a
I'm struggling to find the right terminology here, but if you have jQuery object...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.