I am a beginner with Android.
In Android, some generic elements can be automatically saved/restored in onSaveInstanceState
/onRestoreInstanceState
.
For example, an EditText
saves/restores the Text
property, a RatingBar
saves/restores the Rating
property...
I see from some tests but I can't find anything about this in the documentation.
How can I know what is saved/restored implicitly, without my intervention?
For example, where I can find that the EditText.Text
is automatically saved/restored?
I specifically don't want test all properties.
Edit from JRG answer :
https://developer.android.com/guide/components/activities/activity-lifecycle.html
Save your activity state As your activity begins to stop, the system calls the onSaveInstanceState() method<...> The default implementation of this method saves transient information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView widget.
How I can know what is the default implementation of save/restore?
Second edit after reread JRG answer :
By default, the system uses the Bundle instance state to save information about >each View object in your activity layout (such as the text value entered into >an EditText widget).
The default implementation saves/restores all state of element view.
Android Documentation that explains about saving states and a very good article on saving states in activity and fragment.
Android Documentation:
https://developer.android.com/guide/components/activities/activity-lifecycle.htmlAdditional Article:
https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/enHere is an example to explain you ...
I have created a simple android project which has total 4 data points that will have some value at some point of the time in the app's life cycle.
saveMe
saveMeNot
withid
(has android:id)Here is the sequence of events as per the screenshots.
SAVE
button to set the values for internal variablessaveMe
andsaveMeNot
. AToast
will be displayed that it saved the values for both the variables.android:id
defined in theactivity_main.xml
. Here only Hello will be saved as the EditText where Hello is typed has anandroid:id=@+id/withId
. Another EditText that has text Hi wouldn't be automatically saved by Android as it doesn't have anyandroid:id
. This is what you get for free (provided all your views haveandroid:id
defined). If you have Custom Views that extend View then they also haveandroid:id
defined.saveMe
andsaveMeNot
. You would have to code for it else the state is lost. Like in my example, I have saved state ofsaveMe
and not forsaveMeNot
. This is something you do not get for free i.e. you would have to code for it.CLICK ME
button to view the values ofsaveMe
andsaveMeNot
and you will see onlysaveMe
values are displayed as it was saved by you inonSaveInstanceState
and retrieved inonRestoreInstanceState
Sequence of Steps
Code
AndroidManifest.xml
MainActivity.java
activity_main.xml