9-May-14 (Created: 9-May-14) | More in 'Android Basic UI'

Notes on Instance state: OnRestoreInstanceState

If an activity is to be legitametly closed by a user, then the state that user is willing to discard is the instance state. For example when the user chooses a back button then he/she is informing android that he/she no longer is interested in this activity and the activity can be closed discarding all its state that is not yet saved. So this state that is transitory and only valid for the life of the activity while it is in memory is the instance state.

If the system choses to close the activity, because there is a change in orientation, then the user will expect that transitory (instance) state right back when the activity is restarted. To facilitate this Android calls this onRestoreInstanceState method with a bundle that contains the saved instance state. (See the onSavedInstanceState method explanation).

In contrast to instance state, the persistent state of an activity is something the user expects to see even after the activity finishes and no longer in play. This persistence state may have been created during the activity or may even exist before the activity is created. This type of state, especially when it is created with the help of the activity, must be explicitly saved to an external persistent store like a file. If the activity doesn?t use an explicit ?save? button for such needs, then the ?onPause? method needs to be used to save such implicit persistent state. This is because no method after onPause is guaranteed to be called in case of low memory conditions. You shouldn't rely on the instance state if the information is too important to loose