Sky Kelsey
Sky Kelsey

Reputation: 19290

Parent Activity normally stops on lock, but not after orientation change

I have a demo app with two Activities. Parent is launched when the app starts, and Child is launched via a button in Parent. Child is set up with a transparent background so that Parent doesn't stop when Child launches.

I start the app, which launches Parent, then I start Child. Then I lock the device. I see that the Child calls onStop(), then the Parent calls onStop().

However, if instead of locking the app, I rotate the device, which triggers an orientation change, then when I lock the app only the Child calls onStop(). After I unlock the app, and hit the back button, both Child and Parent call onStop(), so the Parent Activity is in this weird state where it's not visible, but it hasn't called onStop().

  1. Why is this happening?
  2. How can I get Parent to call onStop() when the device is locked?

I've created a simple Android project to demonstrate the problem I'm seeing.

https://github.com/skykelsey/Rotation

Upvotes: 3

Views: 222

Answers (1)

ozbek
ozbek

Reputation: 21183

This is what I am getting on Nexus 5 (Android 5.0.1) if I launch the app -> start ChildActivity -> rotate the device -> lock the device -> unlock the device:

E/ROTATION﹕ ParentActivity.onStart()
E/ROTATION﹕ Orientation: portrait
E/ROTATION﹕ ChildActivity.onStart()
E/ROTATION﹕ Orientation: portrait
E/ROTATION﹕ ChildActivity.onStop()
E/ROTATION﹕ ChildActivity.onStart()
E/ROTATION﹕ Orientation: landscape
E/ROTATION﹕ ParentActivity.onStop()
E/ROTATION﹕ ParentActivity.onStart()
E/ROTATION﹕ Orientation: landscape
E/ROTATION﹕ ChildActivity.onStop()
E/ROTATION﹕ ChildActivity.onStart()
E/ROTATION﹕ Orientation: portrait
E/ROTATION﹕ ParentActivity.onStop()
E/ROTATION﹕ ParentActivity.onStart()
E/ROTATION﹕ Orientation: portrait
E/ROTATION﹕ ChildActivity.onStop()
E/ROTATION﹕ ChildActivity.onStart()
E/ROTATION﹕ Orientation: landscape
E/ROTATION﹕ ParentActivity.onStop()
E/ROTATION﹕ ParentActivity.onStart()
E/ROTATION﹕ Orientation: landscape

i.e., can not reproduce the problem you have described. In fact, it is normal if you do not see the onStop() occasionally as it is not guaranteed to be called.

protected void onStop ()

Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

Also, see the activity life-cycle table provided on that page:

Activity life-cycle table

The highlighted text reads as

Note the "Killable" column in the above table -- for those methods that are marked as being killable, after that method returns the process hosting the activity may killed by the system at any time without another line of its code being executed. Because of this, you should use the onPause() method to write any persistent data (such as user edits) to storage.

That's, if the operation you are performing is important, you should use onPause() rather than onStop().

Hope this helps.

Upvotes: 1

Related Questions