简体   繁体   中英

ListPreference updates its value only when entry is chosen for the second time

When the language in ListPreference is changed, setLanguage() method from this answer in onSharedPreferenceChanged() method in my SettingsFragment class is called.

String defaultCode = getActivity().getString(R.string.string_english_code);
String langCode = sharedPreferences.getString(PREF_LANGUAGE, defaultCode);
setLanguage(getActivity(), langCode);
((SettingsActivity) getActivity()).restartFragment();

Changing a language works fine, but when setSummary() is called on the ListPreference object in onCreate() it changes only when the necessary language is chosen for the second time.

ListPreference langPreference = (ListPreference) findPreference(PREF_LANGUAGE);
langPreference.setSummary(langPreference.getEntry());

PS If setLanguage() is not called, summary updates as it should be.

Instead of

langPreference.setSummary(langPreference.getEntry());

Try

langPreference.setSummary(PreferenceManager.getDefaultSharedPreferences(context).getString("key", "default value");

I use this code to update my Preference summaries, it works everytime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM