Skip to content

Commit c1b0721

Browse files
committed
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
1 parent 9252dbd commit c1b0721

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

core/java/android/preference/TwoStatePreference.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public abstract class TwoStatePreference extends Preference {
3737
private CharSequence mSummaryOn;
3838
private CharSequence mSummaryOff;
3939
boolean mChecked;
40+
private boolean mCheckedSet;
4041
private boolean mSendClickAccessibilityEvent;
4142
private boolean mDisableDependentsState;
4243

@@ -74,11 +75,16 @@ protected void onClick() {
7475
* @param checked The checked state.
7576
*/
7677
public void setChecked(boolean checked) {
77-
if (mChecked != checked) {
78+
// Always persist/notify the first time; don't assume the field's default of false.
79+
final boolean changed = mChecked != checked;
80+
if (changed || !mCheckedSet) {
7881
mChecked = checked;
82+
mCheckedSet = true;
7983
persistBoolean(checked);
80-
notifyDependencyChange(shouldDisableDependents());
81-
notifyChanged();
84+
if (changed) {
85+
notifyDependencyChange(shouldDisableDependents());
86+
notifyChanged();
87+
}
8288
}
8389
}
8490

0 commit comments

Comments
 (0)