Junior — Middle
How does the system save the state of an Activity when the device configuration changes?
sobes.tech AI
Answer from AI
When the device configuration changes (for example, screen rotation), Android destroys the current Activity and recreates it. To preserve state, the system uses lifecycle methods:
- In the
onSaveInstanceState(Bundle outState)method, data can be saved into theBundleobject. - When recreating the Activity, the saved data can be restored in the
onCreate(Bundle savedInstanceState)oronRestoreInstanceState(Bundle savedInstanceState)methods.
Thus, UI state and other necessary data are automatically saved and restored by the system if the developer implements these methods.