Activity.OnSaveInstanceState Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
| OnSaveInstanceState(Bundle) |
Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both). |
| OnSaveInstanceState(Bundle, PersistableBundle) |
This is the same as onSaveInstanceState(Bundle) but is called for activities created with the attribute R.attr.persistableMode set to persistAcrossReboots. |
OnSaveInstanceState(Bundle)
Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).
[Android.Runtime.Register("onSaveInstanceState", "(Landroid/os/Bundle;)V", "GetOnSaveInstanceState_Landroid_os_Bundle_Handler")]
protected virtual void OnSaveInstanceState(Android.OS.Bundle outState);
[<Android.Runtime.Register("onSaveInstanceState", "(Landroid/os/Bundle;)V", "GetOnSaveInstanceState_Landroid_os_Bundle_Handler")>]
abstract member OnSaveInstanceState : Android.OS.Bundle -> unit
override this.OnSaveInstanceState : Android.OS.Bundle -> unit
Parameters
- outState
- Bundle
Bundle: Bundle in which to place your saved state. This value cannot be null.
- Attributes
Remarks
Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both). This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle). Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when the user no longer actively interacts with an activity, or onStop() which is called when activity becomes invisible. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact. The default implementation takes care of most of the UI per-instance state for you by calling View.onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself. If called, this method will occur after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P. For applications targeting earlier platform versions this method will occur before onStop() and there are no guarantees about whether it will occur before or after onPause().
See also:
Android reference for android.app.Activity.onSaveInstanceState.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Applies to
OnSaveInstanceState(Bundle, PersistableBundle)
This is the same as onSaveInstanceState(Bundle) but is called for activities created with the attribute R.attr.persistableMode set to persistAcrossReboots.
[Android.Runtime.Register("onSaveInstanceState", "(Landroid/os/Bundle;Landroid/os/PersistableBundle;)V", "GetOnSaveInstanceState_Landroid_os_Bundle_Landroid_os_PersistableBundle_Handler")]
public virtual void OnSaveInstanceState(Android.OS.Bundle outState, Android.OS.PersistableBundle outPersistentState);
[<Android.Runtime.Register("onSaveInstanceState", "(Landroid/os/Bundle;Landroid/os/PersistableBundle;)V", "GetOnSaveInstanceState_Landroid_os_Bundle_Landroid_os_PersistableBundle_Handler")>]
abstract member OnSaveInstanceState : Android.OS.Bundle * Android.OS.PersistableBundle -> unit
override this.OnSaveInstanceState : Android.OS.Bundle * Android.OS.PersistableBundle -> unit
Parameters
- outState
- Bundle
Bundle: Bundle in which to place your saved state. This value cannot be null.
- outPersistentState
- PersistableBundle
PersistableBundle: State which will be saved across reboots. This value cannot be null.
- Attributes
Remarks
This is the same as onSaveInstanceState(Bundle) but is called for activities created with the attribute R.attr.persistableMode set to persistAcrossReboots. The PersistableBundle passed in will be saved and presented in onCreate(Bundle,PersistableBundle) the first time that this activity is restarted following the next device reboot.
See also:
Android reference for android.app.Activity.onSaveInstanceState.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.