Don't assume a default of 'false' for TwoStatePreference

TwoStatePreference would treat a default value of false as having no
effect since it matched the initial value of the internal field. This
caused asymmetric behavior around persistence and listener
notification when the default was true (persisted) vs. false (not
persisted).

Standard behavior for most Preferences is to persist the default
value. Make TwoStatePreference (CheckBoxPreference and
SwitchPreference) follow this pattern.

Bug 5722690

Change-Id: I8b3cf55efc5dfd573b511913b48ced017f0432d8
This commit is contained in:
Adam Powell
2012-08-10 14:20:14 -07:00
parent 9252dbd314
commit c1b07211b8

View File

@@ -37,6 +37,7 @@ public abstract class TwoStatePreference extends Preference {
private CharSequence mSummaryOn;
private CharSequence mSummaryOff;
boolean mChecked;
private boolean mCheckedSet;
private boolean mSendClickAccessibilityEvent;
private boolean mDisableDependentsState;
@@ -74,11 +75,16 @@ public abstract class TwoStatePreference extends Preference {
* @param checked The checked state.
*/
public void setChecked(boolean checked) {
if (mChecked != checked) {
// Always persist/notify the first time; don't assume the field's default of false.
final boolean changed = mChecked != checked;
if (changed || !mCheckedSet) {
mChecked = checked;
mCheckedSet = true;
persistBoolean(checked);
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
if (changed) {
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
}
}
}