|
30 | 30 | import android.graphics.drawable.Drawable;
|
31 | 31 | import android.os.Build.VERSION;
|
32 | 32 | import android.os.Build.VERSION_CODES;
|
| 33 | +import android.os.Parcel; |
| 34 | +import android.os.Parcelable; |
33 | 35 | import androidx.annotation.ColorInt;
|
34 | 36 | import androidx.annotation.ColorRes;
|
35 | 37 | import androidx.annotation.DimenRes;
|
|
41 | 43 | import androidx.annotation.RequiresApi;
|
42 | 44 | import androidx.annotation.RestrictTo;
|
43 | 45 | import androidx.core.graphics.drawable.DrawableCompat;
|
| 46 | +import androidx.customview.view.AbsSavedState; |
44 | 47 | import androidx.core.view.ViewCompat;
|
45 | 48 | import androidx.core.widget.TextViewCompat;
|
46 | 49 | import androidx.appcompat.content.res.AppCompatResources;
|
@@ -247,6 +250,26 @@ public void onInitializeAccessibilityEvent(@NonNull AccessibilityEvent accessibi
|
247 | 250 | accessibilityEvent.setChecked(isChecked());
|
248 | 251 | }
|
249 | 252 |
|
| 253 | + @NonNull |
| 254 | + @Override |
| 255 | + public Parcelable onSaveInstanceState() { |
| 256 | + Parcelable superState = super.onSaveInstanceState(); |
| 257 | + SavedState savedState = new SavedState(superState); |
| 258 | + savedState.checked = checked; |
| 259 | + return savedState; |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public void onRestoreInstanceState(@Nullable Parcelable state) { |
| 264 | + if (!(state instanceof SavedState)) { |
| 265 | + super.onRestoreInstanceState(state); |
| 266 | + return; |
| 267 | + } |
| 268 | + SavedState savedState = (SavedState) state; |
| 269 | + super.onRestoreInstanceState(savedState.getSuperState()); |
| 270 | + setChecked(savedState.checked); |
| 271 | + } |
| 272 | + |
250 | 273 | /**
|
251 | 274 | * This should be accessed via {@link
|
252 | 275 | * androidx.core.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)}
|
@@ -1051,4 +1074,49 @@ void setShouldDrawSurfaceColorStroke(boolean shouldDrawSurfaceColorStroke) {
|
1051 | 1074 | materialButtonHelper.setShouldDrawSurfaceColorStroke(shouldDrawSurfaceColorStroke);
|
1052 | 1075 | }
|
1053 | 1076 | }
|
| 1077 | + |
| 1078 | + static class SavedState extends AbsSavedState { |
| 1079 | + |
| 1080 | + boolean checked; |
| 1081 | + |
| 1082 | + public SavedState(Parcelable superState) { |
| 1083 | + super(superState); |
| 1084 | + } |
| 1085 | + |
| 1086 | + public SavedState(@NonNull Parcel source, ClassLoader loader) { |
| 1087 | + super(source, loader); |
| 1088 | + readFromParcel(source); |
| 1089 | + } |
| 1090 | + |
| 1091 | + @Override |
| 1092 | + public void writeToParcel(@NonNull Parcel out, int flags) { |
| 1093 | + super.writeToParcel(out, flags); |
| 1094 | + out.writeInt(checked ? 1 : 0); |
| 1095 | + } |
| 1096 | + |
| 1097 | + private void readFromParcel(@NonNull Parcel in) { |
| 1098 | + checked = in.readInt() == 1; |
| 1099 | + } |
| 1100 | + |
| 1101 | + public static final Creator<SavedState> CREATOR = |
| 1102 | + new ClassLoaderCreator<SavedState>() { |
| 1103 | + @NonNull |
| 1104 | + @Override |
| 1105 | + public SavedState createFromParcel(@NonNull Parcel in, ClassLoader loader) { |
| 1106 | + return new SavedState(in, loader); |
| 1107 | + } |
| 1108 | + |
| 1109 | + @NonNull |
| 1110 | + @Override |
| 1111 | + public SavedState createFromParcel(@NonNull Parcel in) { |
| 1112 | + return new SavedState(in, null); |
| 1113 | + } |
| 1114 | + |
| 1115 | + @NonNull |
| 1116 | + @Override |
| 1117 | + public SavedState[] newArray(int size) { |
| 1118 | + return new SavedState[size]; |
| 1119 | + } |
| 1120 | + }; |
| 1121 | + } |
1054 | 1122 | }
|
0 commit comments