Android - Activity State Save

Suppose we are using android application having an EditText control and then if we write something into it and rotate the application. Then with changing the orientation of the android application, the data will be lost and the EditTest field will become empty.

Here you may notice that with screen orientation change, the EditTest field becomes empty. And the key point is, here is no id defined for the EditTest field.
Here there is an id for the EditText field and the information persisted with changing orientation. 

Actually when we change the orientation of an activity, then execution happens with the following sequences:
That means after onResume() it calls onPause() --> onStop()
And then again onStart() and onResume() 

At onStart() the activity try to restore data from the previous state of orientation. If id is not defined then it fails to restore the data.

Basically, this data save is done using the onSaveInstanceState() method and then onRestoreInstanceState() to restore the data. When onPause() is called then onSaveInstance() is also called, similarly when onStart() is called then onRestoreInstanceState() is also called. 

Always try to call super.onSaveInstanceState() to save view data to Activity and onRestoreInstanceState() to restore it.







Comments