9

From this example:

private static Preference.OnPreferenceClickListener BindToPreferenceClickListener = new Preference.OnPreferenceClickListener() {
    public boolean onPreferenceClick(Preference preference) {
        boolean isHandled = false;
        if (...) {
           isHandled = true;
        }
        return isHandled;
    }
}

What does the return value of 'true' do? The API documentation states that the value indicates that the click was handled if 'true', but what does that really mean? I thought it meant that the editor for the passed preference was not called and a custom editor could be used instead, but my experimentation concludes that the default editor is called in either state.

2 Answers 2

10

As the OnPreferenceChangeListener.onPreferenceChange method documentation says:

Returns True to update the state of the Preference with the new value.

The method is run before persisting the new value to the preferences. If you return false, the value is not persisted. If you return true, it is persisted.

1
  • 1
    This should be accepted as correct answer. There's can be only one listener for each preference object. If, for example, you've decided that the user unchecked "personalize your ads" by mistake, you can disregard that click with a simple false return here. Mar 8, 2019 at 19:46
7

It means that when someone clicks the preference your listener has a chance to deal with the click.

If you handle the click you return true.

If you do not handle the click i.e. you don't do anything when this is clicked you can return false and the system will then pass the click on to the next listener that is listening for an on click for that preference.

I can't think of an example where you would listen for the click then not handle it sorry.

1
  • 1
    but it's not addOnPreferenceClickListener. it's setOnPreferenceClickListener. there can't be other listeners... Jul 21, 2015 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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