Search code examples
javaandroidsharedpreferencescheckboxpreference

CheckBoxPreference - onSharedPreferenceChanged method not getting called


I have a couple of CheckBoxPreferences set up, my preference class extends PreferenceActivity and implements OnSharedPreferenceChangeListener This is what I'm using to respond to people checking/unchecking the CheckBoxPreferences:

public void onSharedPreferenceChanged(SharedPreferences P, String K) {
    if (K.equals(CheckBoxPref_KEY_HERE)) {
        MyClass.BooleanVariable = P.getBoolean("CheckBoxPref_KEY_HERE", true);
    }
}

As far as I can tell, the onSharedPreferenceChanged method above is never even getting called?


Solution

  • checkBoxPreference = (CheckBoxPreference) this.findPreference("CheckBoxPref_KEY_HERE");
    
    checkBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
                // do your work here
                return true;
            }
        });