ZOMGbies
ZOMGbies

Reputation: 51

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?

Upvotes: 1

Views: 2221

Answers (2)

havexz
havexz

Reputation: 9590

Here is the soln if you want to do something on all the preferences:

Create a class member:

SharedPreferences settings;

in your onCreate method:

settings = getSharedPreferences(<your_pref_name>, 0);
settings.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
  @Override
  public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {
    // Do whatever
  }
});

Upvotes: 1

gwvatieri
gwvatieri

Reputation: 5173

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;
        }
    });

Upvotes: 0

Related Questions