How to listen for preference changes within a PreferenceFragment?

前端 未结 9 2096
自闭症患者
自闭症患者 2020-11-28 19:16

As described here, I am subclassing PreferenceFragment and displaying it inside an Activity. That document explains how to listen for preference changes here, but only if yo

9条回答
  •  星月不相逢
    2020-11-28 19:38

    I recently finished putting together my own PreferenceScreen using the Preferences API, so I thought I would contribute my own full Example.. This includes updating the Summary to new/changed Value, as well as Listening for and reacting to changes.

    PS. To answer your last question: To show a default Value for Summary upon initial creation of the PreferenceScreen (prior to any change in the Value), you could simply set the android:summary to a Value of your choosing, from within the preferences.xml file directly - and then, once there is a change in the Value, it will update automatically by using the code contained in my Example below. Personally, I use a short explanation of the Preference as my initial Summary, set within my preferences.xml, and then once the Value does get changed for the first time, it will simply show the current Value as the Summary from then on..

    Anyhow, here is my full Example:

    SettingsFragment.java

    public class SettingsFragment extends PreferenceFragment {
    
    public static final String PREF_NOTIFICATION_MODE = "pref_notificationMode";
    private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener;
    
    @Override
    public void onCreate (@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    
        final SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
    
        preferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    
                if (key.equals(PREF_NOTIFICATION_MODE)) {
                    Preference notifModePref = findPreference(key);
                    notifModePref.setSummary(sharedPreferences.getString(key, ""));
    
                    //  DO SOMETHING ELSE HERE WHEN (PREF_NOTIFICATION_MODE) IS CHANGED
                }
            }
        };
    }
    
    @Override
    public void onResume() {
        super.onResume();
    
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(preferenceChangeListener);
    
        Preference notifModePref = findPreference(PREF_NOTIFICATION_MODE);
        notifModePref.setSummary(getPreferenceScreen().getSharedPreferences().getString(PREF_NOTIFICATION_MODE, ""));
    
    }
    
    @Override
    public void onPause() {
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
    
        super.onPause();
    }
    }
    


    I hope this helps!
    Any positive feedback is greatly appreciated, as I'm fairly new to StackOverflow ;)
    Happy coding!

提交回复
热议问题