How do you refresh PreferenceActivity to show changes in the settings? How do you refresh PreferenceActivity to show changes in the settings? android android

How do you refresh PreferenceActivity to show changes in the settings?


Instead of

finish();startActivity(getIntent());

I prefer the following code :

setPreferenceScreen(null);addPreferencesFromResource(R.xml.preferences);

This will reset the display as well but without the whole finish procedure and its consequences.Plus this code works well with PreferenceActivity and PreferenceFragment.

This is interesting if you would like to change dynamically the locale value for example. In this case, working with the manager is not enough because you want a full reload of titles and values.

EDIT: setPreferenceScreen is deprecated in PreferenceActivity (still working) but is not in PreferenceFragment


Nikolay's answer is correct. I just want to add some code here to illustrate his point more clearly.

private CheckBoxPreference mOn15Past;private CheckBoxPreference mOn30Past;private CheckBoxPreference mOn45Past;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    // Load the preferences from an XML resource    addPreferencesFromResource(R.xml.preferences);    mOn15Past = (CheckBoxPreference) findPreference("ChimeOn15Past");    mOn30Past = (CheckBoxPreference) findPreference("ChimeOn30Past");    mOn45Past = (CheckBoxPreference) findPreference("ChimeOn45Past");    final Preference chimeMaster = findPreference("ChimeMaster");    chimeMaster.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {        @Override        public boolean onPreferenceChange(Preference preference, Object newVal) {            final boolean value = (Boolean) newVal;            mOn15Past.setChecked(value);            mOn30Past.setChecked(value);            mOn45Past.setChecked(value);            return true;        }    });}

In short, PreferenceActivity is not designed to refresh its values from the persistent storage once it is started. Instead of using SharedPreferences.Editor to modify and commit additional changes like you did in your code, it is better to make the changes into the local PreferenceManager object which will be committed by the PreferenceActivity in its normal lifecycle.


There's a simple method to refresh all items on the list at once. Simply do the following:

getPreferenceScreen().removeAll();addPreferencesFromResource(R.xml.preferences);

With this approach you won't loose ListView's position.