onSaveInstanceState
onSaveInstanceState is a callback in Android that allows an activity or fragment to save transient UI state before it may be destroyed by the system. It is typically invoked when the user rotates the device, switches away from the app, or the system replaces the process to reclaim memory. The method receives a Bundle where you can store small pieces of information that are needed to restore the user interface to its previous state.
In an activity, you override onSaveInstanceState(Bundle outState) and put values into the bundle using methods like
Best practices emphasize saving only lightweight, transient UI state rather than large data or persistent records.
Note that onSaveInstanceState is not guaranteed to be called in all shutdown scenarios, such as a process