11

I want to get value of a checkboxpreference. which method is suitable for listen to preference?

And what's real difference between OnPreferenceChangeListener and OnSharedPreferenceChangeListener ?!

UPDATE Why onSharedPreferenceChanged not called?

public class Setting extends PreferenceActivity implements
        OnSharedPreferenceChangeListener // ,OnPreferenceChangeListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);// deprecated warning
    }

    @Override
    public void onSharedPreferenceChanged(
            SharedPreferences sharedPreferences, String key) {// DO Stuff
    }

    @Override
    protected void onResume() {
        super.onResume();
        // getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
        // PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
        this.getSharedPreferences("myPrefDB", MODE_PRIVATE)
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        // PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
        this.getSharedPreferences("myPrefDB", MODE_PRIVATE)
                .unregisterOnSharedPreferenceChangeListener(this);
    }
}

1 Answer 1

14

The difference between these 2 is that OnPreferenceChangeListener is a callback called only when its preference changes (applies to a single key), while OnSharedPreferenceChange is a callback called whenever any of the preferences in that SharedPreferences object changes (applies to all keys).

So, in your case, you need to use the OnPreferenceChangeListener with your CheckBoxPreference.

Here is an example:

Preference ckboxPref = this.findPreference(CKBOX_PREF_KEY);
ckboxPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

    @Override
    public boolean onPreferenceChange(Preference preference,
            Object newValue) {
        //Do stuff
    }
});

The method findPreference does not work if you use fragments, but you aren't using any so it is fine.

10
  • Why it's not work? : @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {if (key.equalsIgnoreCase("myKey") ...
    – Dr.jacky
    Nov 10, 2012 at 11:51
  • What doesn't work? It's not called when you change that preference?
    – Jong
    Nov 10, 2012 at 11:57
  • Do you call it on the SharedPreferences object that contains this key? Add the code in your question, please.
    – Jong
    Nov 10, 2012 at 12:03
  • It doesn't call because (Probably) "myPrefDB" is not the name your PreferenceActivity uses.
    – Jong
    Nov 10, 2012 at 13:09
  • 2
    No, I don't think you need to unregister when the activity is paused. After all, the preference won't changed when the activity is paused because it is only changed in this activity...
    – Jong
    Nov 10, 2012 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.