Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are ALL the cases when the onSaveInstanceState() method called?

Tags:

java

android

All the sources I read have all mentioned couple of cases and concluded with "a few other cases". What are ALL the cases when the onSaveInstanceState method called in a View/Activity?

like image 703
Raj Avatar asked Jul 13 '11 11:07

Raj


1 Answers

  • onSaveInstanceState() is called when there is an orientation change or user presses home button.
  • If there is an another activity in front of an activity and the OS kills the hidden activity in order to free memory(or when memory is needed elsewhere), then onSaveInstanceState() is called so activity can save its state information which is restored using onRestoreInstanceState() when user start that activity next time .
  • Default views of Android save their state via a call to View.onSaveInstanceState which is restored by the default implementation of onRestoreInstanceState

As per doc

If the user interacts with an activity and presses the Back button or if the finish() method of an activity is called, the activity is removed from the current activity stack and recycled. In this case there is no instance state to save and the onSaveInstanceState() method is not called.

If the user interacts with an activity and presses the Home button, the activity instance state must be saved. The onSaveInstanceState() method is called. If the user restarts the application it will resume or restart the last running activity. If it restarts the activity it provides the bundle with the save data to the onRestoreInstanceState() and onCreate() methods.

If you override onSaveInstanceState() and onRestoreInstanceState() you should call the super implementation of it, because the default views of Android store their data via a call to View.onSaveInstanceState from the onSaveInstanceState() method of the activity. For example EditText stores its content via the default call of this method.

like image 184
Android Developer Avatar answered Oct 06 '22 01:10

Android Developer