Android中哪些View属性可自动保存?LinearLayout旋转后背景色未恢复
Hey there! Glad you already got the manual state restoration working for your LinearLayout's background color—let's dive into exactly which View properties the Android system handles automatically out of the box.
First, a quick recap: the system only saves state for Views that have a unique android:id assigned, as you already noted. It focuses on preserving user-interactive state or core functional values that users would expect to persist across configuration changes (like screen rotations).
Here's a breakdown of the most common automatically saved properties by View type:
1. Input & Selection Views
- EditText: Text content, cursor position, and selection range
- CheckBox/RadioButton/Switch/ToggleButton: Checked/activated state
- Spinner: Selected item position
- AutoCompleteTextView: Entered text and dropdown selection state
2. Scrollable Views
- ScrollView/HorizontalScrollView: Vertical/horizontal scroll offset
- ListView/GridView: Scroll position (including the top visible item index and offset)
- RecyclerView: Scroll position (via
LayoutManagerstate, which the system saves if the RecyclerView has an ID)
3. Value-Adjustment Views
- SeekBar: Current progress value
- RatingBar: Current rating value
4. Editable Text Views
- Any
TextViewmarked as editable (e.g., legacy usage withandroid:editable="true"—modern apps typically use EditText instead): Text content and cursor position
Now, why didn't your LinearLayout's background color get saved? The system doesn't automatically persist style or layout properties like background color, padding, or visibility unless they're tied to a user-triggered state change. Background color is considered a static styling attribute (even if you modified it programmatically), so it falls outside the system's default state-saving scope. That's why you had to handle it manually—great job on that, by the way!
One quick bonus tip: If you're building a custom View and want its custom state to be saved automatically, you can override onSaveInstanceState() to bundle your state into a Parcelable object, then restore it in onRestoreInstanceState(Parcelable state). This lets the system handle the state persistence just like it does for built-in Views.
内容的提问来源于stack exchange,提问作者pablo.vix

