1

I have an if loop when a chebkox in sharedpreferences changed status. But I'm not able to set putBoolean("blabla", false); when the status changed. For example: User hits checkbox, CB gets checked, if says uncheck it but it won't uncheck.

My Code:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {    
        if(key.equals("cbSaveUsername")) {
            SharedPreferences.Editor SFEdit2 = sharedPreferences.edit();
            SFEdit2.putBoolean("cbSaveUsername", false);
            SFEdit2.commit();
        }
}

Can anyone tell me my mistake?

Edit: It looks like it works programatically but the tick is still in the box :o Where could be the mistake? I use it like Gunaseelan postet

Phil
  • 23
  • 5

1 Answers1

1

Try to use onPause() and onResume() methods.

@Override
protected void onResume() {
    super.onResume();
    // Set up a listener whenever a key changes
    getPreferenceScreen().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onPause() {
    super.onPause();
    // Unregister the listener whenever a key changes
    getPreferenceScreen().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
}

For more details onSharedPreferenceChanged not fired if change occurs in separate activity?

Community
  • 1
  • 1
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • Mhm I replaced the `getPreferenceScreen().getSharedPreferences()` by `PreferenceManager.getDefaultSharedPreferences(getBaseContext())` because your's is deprecated. But it won't work :/ – Phil Jun 08 '13 at 08:15
  • Also refer [here](http://stackoverflow.com/questions/531427/how-do-i-display-the-current-value-of-an-android-preference-in-the-preference-su) – Gunaseelan Jun 08 '13 at 08:55
  • did you [register](http://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently/3104265#3104265) with onSharedPreferenceChanged? – Gunaseelan Jun 08 '13 at 09:14
  • yep, in `onCreate()` I did `SP.registerOnSharedPreferenceChangeListener(this);` – Phil Jun 08 '13 at 09:31
  • `SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(this); SP.registerOnSharedPreferenceChangeListener(this);` Like this? – Gunaseelan Jun 08 '13 at 10:16
  • right ;) and it seems to be just a visual thing, when I do System.out.println and put out the boolean after the change it gives the correct, but the tick is still in the box, even if the value is `false` – Phil Jun 08 '13 at 10:27
  • Ah! Okay okay got it. Just use `cb.setChecked(false);` manually if your boolean is `false`. – Gunaseelan Jun 08 '13 at 10:39
  • But how can I get the checkbox? It doesnt work with `findViewById` because it is in preferences – Phil Jun 08 '13 at 21:02
  • ref [here](http://stackoverflow.com/questions/8373131/checkboxpreference-onsharedpreferencechanged-method-not-getting-called) – Gunaseelan Jun 09 '13 at 01:57
  • `findPreference()` is deprecated – Phil Jun 09 '13 at 07:08
  • okay.. Ref [here](http://stackoverflow.com/questions/6503496/preferences-without-deprecated-methods) and [here](http://stackoverflow.com/questions/11272839/non-deprecated-findpreference-method-android). Surely you will get an idea. – Gunaseelan Jun 09 '13 at 09:30