1

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?

Drahakar
  • 5,986
  • 6
  • 43
  • 58
ZOMGbies
  • 51
  • 1
  • 1
  • 7
  • Much apreciated! I almost had it sussed with your help :) I'm not certain how to change the value of `MainClass.BooleanVariable`... I have: `MainClass.BooleanVariable = P.isEnabled();` but that seems to **always** enable? o_O – ZOMGbies Dec 04 '11 at 04:28

2 Answers2

1

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
  }
});
havexz
  • 9,550
  • 2
  • 33
  • 29
  • Here is the unregister api http://developer.android.com/reference/android/content/SharedPreferences.html#unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener) – havexz Nov 11 '12 at 03:51
  • Totally,not work for me.here is my question http://stackoverflow.com/questions/13321637/whats-different-between-onpreferencechangelistener-and-onsharedpreferencechange/ – Dr.jacky Nov 11 '12 at 06:04
0
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;
        }
    });
gwvatieri
  • 5,133
  • 1
  • 29
  • 29