Is the method onSaveInstanceState(Bundle) called after onPause()?
Asked Answered
G

2

6

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?

Gowan answered 24/8, 2015 at 7:8 Comment(3)
You should read about the activiy life cycle. Here's a diagram: i.sstatic.net/EVuKo.pngMakebelieve
Thank you, dazito. I think I just got your point with the picture: onPause() itself is ensured to be called before system kill the Activity, so that's why onSaveInstanceState(Bundle) is ensured too. But I noticed that onStop() will certainly be called for less times than onPause() in one lifecycle, and onStop() is also ensured to be called before system kill the Activity. So why onSaveInstanceState(Bundle) always couples with onPause() but not onStop()? Why should onSaveInstanceState(Bundle) be called so many times? It seems to be kind of waste of resource to system.Gowan
Because you can have another activity taking the place (on the screen) of your current activity, therefore your onPause() is called. From Android Docs: For 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 etcMakebelieve
P
1

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 in onCreate(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 in onPause() instead of onSaveInstanceState(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.

Predator answered 24/8, 2015 at 7:15 Comment(0)
G
2

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().

Georgiegeorgina answered 18/12, 2015 at 8:58 Comment(1)
As an important note, 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
P
1

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 in onCreate(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 in onPause() instead of onSaveInstanceState(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.

Predator answered 24/8, 2015 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.