onSaveInstanceState()默认保存内容及对应官方文档位置咨询
Understanding
onSaveInstanceState() Default Behavior in Android It’s totally fair to say the official Android developer docs are a bit vague on the exact scope of what onSaveInstanceState() saves by default. From hands-on testing and community knowledge, here’s a clear breakdown of the key content it handles:
- Activity launch Intent: This is confirmed through real-world testing—when your Activity is destroyed and recreated by the system, the original Intent that launched it is preserved and can be retrieved again via
getIntent(). - State of view hierarchy elements with assigned IDs: Any UI component in your Activity’s view tree that has an
android:idattribute will have its state automatically saved. Examples include text in anEditText, scroll position of aListView, checked state of aCheckBox, and selected item in aSpinner. - State of Fragments not in the back stack: Based on observed system behavior, it’s a solid assumption that Fragments not added to the back stack (i.e., those not using
addToBackStack()during transaction) will have their state saved and restored by the defaultonSaveInstanceState()implementation. Fragments in the back stack are managed separately by the back stack mechanism.
Remember, if you need to save custom data (like member variables holding app state), you’ll still need to override onSaveInstanceState() and manually put those values into the provided Bundle—the default implementation only covers the UI and system-related state mentioned above.
内容的提问来源于stack exchange,提问作者Radu Stefan Popescu
相关产品推荐
相关产品推荐

