简体   繁体   中英

Android adding OnPreferenceChangeListener to MultiSelectListPreference(MSLP) stops MSLP from working

My minimum SDK is Android 4.0. When I try to add an OnPreferenceChangeListener to MultiSelectListPreference, it stops MSLP from storing the changed values. It works fine without the listener, and even with my code commented out it seems to fail.

private void init () {
    MultiSelectListPreference multiSelectListPref = (MultiSelectListPreference) findPreference("repeat_days");
    if (multiSelectListPref != null) {

/* Works fine if this is commented out
            multiSelectListPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    //formatSummary((MultiSelectListPreference) preference, newValue);
                    return false;
                }
            });*/
        }
    }

I need to know when the user changes the information. I have seen this response MultiSelectListPreference not storing values? but I can not seem to get even that to work.

Any help is appreciated, Thanks in advance!

You can view google's code here ( https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/preference/MultiSelectListPreference.java ). The pertinent method is:

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (positiveResult && mPreferenceChanged) {
        final Set<String> values = mNewValues;
        if (callChangeListener(values)) {
            setValues(values);
        }
    }
    mPreferenceChanged = false;
}

So when you return false in your onPreferenceChange() method, you are explicitly telling it not to save your updated values. Return true and this should do what you expect.

The other SO posts suggest there have been various bugs in this preference implementation at various SDK levels. YMMV.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM