Share via


Activity.OnSaveInstanceState Method

Definition

Overloads

OnSaveInstanceState(Bundle)

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in #onCreate or #onRestoreInstanceState (the Bundle populated by this method will be passed to both).

OnSaveInstanceState(Bundle, PersistableBundle)

This is the same as #onSaveInstanceState but is called for activities created with the attribute android.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 or #onRestoreInstanceState (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 in which to place your saved state.

Attributes

Remarks

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in #onCreate or #onRestoreInstanceState (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 or #onRestoreInstanceState.

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 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 is when activity B is launched in front of activity A: the system may avoid calling #onSaveInstanceState 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 android.view.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). 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 android.os.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.

Java documentation for android.app.Activity.onSaveInstanceState(android.os.Bundle).

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.

See also

Applies to

OnSaveInstanceState(Bundle, PersistableBundle)

This is the same as #onSaveInstanceState but is called for activities created with the attribute android.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 in which to place your saved state.

outPersistentState
PersistableBundle

State which will be saved across reboots.

Attributes

Remarks

This is the same as #onSaveInstanceState but is called for activities created with the attribute android.R.attr#persistableMode set to persistAcrossReboots. The android.os.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.

Java documentation for android.app.Activity.onSaveInstanceState(android.os.Bundle, android.os.PersistableBundle).

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.

See also

Applies to