Activity instance state Save and Restore - Tuong-Nguyen/Android GitHub Wiki

When an activity instance is deleted, its state can be saved and restored when it is created again. For example, when changing the device orientation, the activity is deleted and created again. We can use this technique for keeping the state of the activity.

In Activity class, override onSaveInstanceState and onRestoreInstanceState methods

Save the activity state

@override
protected void onSaveInstanceState(Bundle outState){
   // Save instance variable into Bundle
   ...

   super.onSaveInstanceState(outState);
}

Restore the activity state

@override
protected void onRestoreInstanceState(Bundle savedInstanceState){
   super.onRestoreInstanceState(savedInstanceState);

   // Read data in Bundle and put to instance variables
   ...   
}