3

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

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • may this answer help you http://stackoverflow.com/questions/8055658/android-keep-camera-led-on-after-screen-turns-off – Md Mohsin Feb 10 '15 at 05:30

1 Answers1

1

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.

ozbek
  • 20,955
  • 5
  • 61
  • 84
  • Thanks ozbek. I'm surprised you aren't seeing the behavior. I am seeing it every time. Did you somehow change orientation while the device was locked? I made sure to keep the orientation constant when locking an unlocking. I tested this on a Nexus 5 with 4.4.4. I understand that under certain conditions onStop() isn't called, but for me, this is happening every single time. I could use onPause(), but I would rather keep track of situations where the Activity isn't visible at all than simply partially obscured. – Sky Kelsey Feb 14 '15 at 21:10
  • But the parent activity is not killable because it is visible under the child activity, or not? – Eugen Pechanec Feb 16 '15 at 13:37
  • @SkyKelsey: no, I didn't change the device orientation. However, the screen lock (PIN) was enabled when I tested. Maybe that's effect, but I can't test it now (traveling, sorry). The main point is that you can not rely on `onStop`. – ozbek Feb 16 '15 at 14:12
  • @EugenPechanec: negative, both parent and child activities are invisible when scree is off and/or lock screen is visible. – ozbek Feb 16 '15 at 14:13
  • 1
    I'm going to award the bounty to you for running the example project and giving it a shot, even though I don't feel like this answers the question. – Sky Kelsey Feb 17 '15 at 04:24