Activity And The Saved Instance State

by Anastassia Elias / from beautifuldecay.com

When we are using an application, fairly easily may happens to go to another one to do something, for example see a message, and after we come back. So, in this common situation, when we come back to the first application, we are expecting to see all thing exactly as we left. Imagine if you were searching a person on a social network and after see 20 perfils in a list you must to answer an email urgently in another application and when you come back everything is restarted. That is because the activity save an instance state when it lefts to be the first one on the activity stack.

“The onSaveInstanceState() can be used to store this instance state as a Bundle. A Bundle can contain primitive data types, arrays, String and objects which are of the Parcelable or Serialisable type.”(VOGELLA on http://www.vogella.com/tutorials/AndroidLifeCycle/article.html)

The method onSaveInstanceState has the role of store as a Bundle the state of the activity when it is inactived, listing all necessary configurations of the view to provide the same view, showing the same thing that before when the activity is reinitialized.

“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.” (VOGELLA on http://www.vogella.com/tutorials/AndroidLifeCycle/article.html)

As Vogella said, the state of an Activity is saved only when the activity is stopped or paused. If the activity is destroyed, it is removed from activity stack and the state is not saved. When the stopped or paused activity get active again, the saved state as a Bundle is provided to method onRestoreInstaceState() that restore the activity following the last state, and after it, the same bundle is provided to method onCreate().

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s