Herr von Wurst
Herr von Wurst

Reputation: 2621

Programatically updated ListPreferences is not displayed

When the user updates a shared preference through a subclass of PreferenceActivity I check whether or not the new value is valid at the given time. If not the value should be changed back in the onSharedPreferenceChanged method.

This works so far. I set an OnSharedPreferenceChangedListener, the method gets called. The user-set value will be overwritten and the new value will be used in the app, however when I open this specific preferences value (in this case a ListPreference) again the wrong list item will be selected (the one the user selected, not the one set in the Listener). I tried overwriting the value with both:

mPrefs.edit().putString("answers", value.toString()).commit();
mPrefs.edit().putString("answers", value.toString()).apply();

Are there additional steps I need to take to update the ListPreference? After restarting the PreferenceActivity the value will be displayed correctly.

Upvotes: 0

Views: 110

Answers (2)

Herr von Wurst
Herr von Wurst

Reputation: 2621

I found a way to solve this issue by setting the value manually as described here:

http://liquidlabs.ca/2011/08/25/update-preference-value-without-reloading-preferenceactivity/

Upvotes: 0

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

try to use Override method SharedPreferenceChanged

@Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                String key) {           
            if (key.equals(KEY)) {
                Preference ServicePref = findPreference(key);
                // Set summary to be the user-description for the selected value         

                SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);                 

            }           
    }

Upvotes: 1

Related Questions