Sobes.tech
Junior — Middle

In what situations does the system call the onSaveInstanceState method of an Activity?

sobes.tech AI

Answer from AI

The onSaveInstanceState method in Android's Activity is called by the system in situations where it needs to save the current state of the activity before it might be destroyed.

Typical cases of invocation:

  • During screen rotation (configuration change), when the Activity will be recreated.
  • When the activity goes to the background, and the system may free resources and destroy the activity.

In onSaveInstanceState, you can save data into the Bundle object to restore them later in onCreate or onRestoreInstanceState.

Example of saving state:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("key", "value");
}

This helps preserve user data and UI state during configuration changes or temporary activity destruction.

In what situations does the system call the… - sobes.tech