I'm new to android and I read a book for beginners which said that onSaveInstanceState(Bundle)
is ensured to be called before the system reclaims your Activity
.
I tried it on some test codes and found it incorrect. I found that onSaveInstanceState(Bundle)
was called every time after onPause()
was called. And it has nothing to do with system reclaimation.
I'm not very sure about it, so that's the question: when is onSaveInstanceState(Bundle)
called actually?
According to Android Documentation:
In addition, the method
onSaveInstanceState(Bundle)
is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received inonCreate(Bundle)
if the activity needs to be re-created. See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting. Note that it is important to save persistent data inonPause()
instead ofonSaveInstanceState(Bundle)
because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.
Yes onPause()
is called before onSaveInstanceState(Bundle)
. But onPause()
is guaranteed to be called as its a part of activity life cycle
Usually when your activity is re-created for example when you change device orientation then onSaveInstanceState(Bundle)
is called if you have not specified the android:configChanges
tag in your manifest.xml
file.
I don't agree with a previous answer.
According to the Documentation:
If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().
onSaveInstanceState()
may get called before onPause()
. It might be due to the fact that the (support) Fragment I'm using is in the ViewPager
. Hope this is helpful for someone else. –
Cryptoclastic According to Android Documentation:
In addition, the method
onSaveInstanceState(Bundle)
is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received inonCreate(Bundle)
if the activity needs to be re-created. See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting. Note that it is important to save persistent data inonPause()
instead ofonSaveInstanceState(Bundle)
because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.
Yes onPause()
is called before onSaveInstanceState(Bundle)
. But onPause()
is guaranteed to be called as its a part of activity life cycle
Usually when your activity is re-created for example when you change device orientation then onSaveInstanceState(Bundle)
is called if you have not specified the android:configChanges
tag in your manifest.xml
file.
© 2022 - 2024 — McMap. All rights reserved.
onPause()
itself is ensured to be called before system kill the Activity, so that's whyonSaveInstanceState(Bundle)
is ensured too. But I noticed thatonStop()
will certainly be called for less times thanonPause()
in one lifecycle, andonStop()
is also ensured to be called before system kill the Activity. So whyonSaveInstanceState(Bundle)
always couples withonPause()
but notonStop()
? Why shouldonSaveInstanceState(Bundle)
be called so many times? It seems to be kind of waste of resource to system. – GowanFor example, when a semi-transparent activity opens (such as one in the style of a dialog), the previous activity pauses. As long as the activity is still partially visible but currently not the activity in focus, it remains paused.
developer.android.com/training/basics/activity-lifecycle/… I recommend you to override all activity life cycles and play around so you can how the activity is paused/resumed/stopped/destroyed etc – Makebelieve