Skip to content Skip to sidebar Skip to footer

How Do You Refresh Preferenceactivity To Show Changes In The Settings?

Based on the following code, can you tell me how to refresh the PreferenceActivity window to show changes in the settings immediately? For example: the user taps the master chime t

Solution 1:

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

Solution 2:

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

privateCheckBoxPreference mOn15Past;
privateCheckBoxPreference mOn30Past;
privateCheckBoxPreference mOn45Past;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resourceaddPreferencesFromResource(R.xml.preferences);

    mOn15Past = (CheckBoxPreference) findPreference("ChimeOn15Past");
    mOn30Past = (CheckBoxPreference) findPreference("ChimeOn30Past");
    mOn45Past = (CheckBoxPreference) findPreference("ChimeOn45Past");

    final Preference chimeMaster = findPreference("ChimeMaster");
    chimeMaster.setOnPreferenceChangeListener(newOnPreferenceChangeListener() {
        @OverridepublicbooleanonPreferenceChange(Preference preference, Object newVal) {
            final boolean value = (Boolean) newVal;
            mOn15Past.setChecked(value);
            mOn30Past.setChecked(value);
            mOn45Past.setChecked(value);
            returntrue;
        }

    });
}

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.

Solution 3:

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.

Solution 4:

Implement onPreferenceChange and toggle related perfs from there. To 'refresh' the whole activity, you need to finish() and restart it.

Solution 5:

You can simply call onCreate method. If you want you can call onStart and onResume methods.

onCreate(null);

I have solved my problem with this way.

Post a Comment for "How Do You Refresh Preferenceactivity To Show Changes In The Settings?"