5

So I just realized that onStop is getting called in my Activity when the power button is clicked to turn off the screen. Previously only onPause was called. Was this a KitKat change and are there notes about it anywhere (was it intentional)? Is this a KitKat change or something that specific manufacturers implemented?

EDIT: I'll be updating this soon with more info. I think the change was more subtle than I first realized, possibly due to me holding a partial wake lock or listening for GPS updates. Regardless, all I know is that in my code, prior to KitKat, onStop was not called when the power button was clicked. Perhaps this is also device dependent.

EDIT: New information. With the following settings, onStop() is not called when the power button is clicked: Android minSDKVersion=4, and targetSDKVersion=8, (if using Android Studio, set compileSdkVersion=8 as well). Verified this on 2 devices (running KitKat and JellyBean) So this issue is not KitKat as first mentioned in the original, but rather the min,target sdk settings. Bounty will be awarded to whomever can find references to when it changed or at least show the first min/target sdk setting that changed the behavior to call onStop from a power button click.

Fraggle
  • 8,607
  • 7
  • 54
  • 86
  • There are a couple of other articles about how this sort of thing can happen when you do not handle orientation changes. http://stackoverflow.com/questions/9948315/activity-lifecycle-x-power-button-x-lock-screen – Brian S Jul 22 '14 at 12:38

5 Answers5

5

The change in lifecycle came with Honeycomb, or when changing an app that targeted sdk 10 or lower to one that targets sdk 11 or higher.

If an Android project declares a targetSDKversion of 10 or less, then when the power button is clicked, onPause is called, but not onStop. This is contrary to what many people think. With targetSDKversion = 11 or higher, however, onPause then onStop will be called when the power button is clicked.

It should be noted, that in some documents, it states that onStop will be "Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. " (emphasis added) but in many other places, it simply states that onStop will be called whenever the current activity is no longer visible. So prior to SDK 11 perhaps the power button click was purposely meant to just call onPause because no other activity was covering the current one. The screen was simply off. Then with Honeycomb they changed the implementation to match the other interpretation (no longer visible).

Fraggle
  • 8,607
  • 7
  • 54
  • 86
0

IMO, Kitkat has nothing to do with it. onStop() will be called whenever your activity is no longer visible to the user. When your screen goes off (e.g. when you press the power button to lock the screen), the onPause() would be called first. Then, the onStop() should get called. But, it does depend on the memory situation of the device. If you are using a low memory device which cannot provide enough memory to keep your activity's process running, the onStop() may not be called. You may see that only onPause() is called in that situation. For reference: http://developer.android.com/reference/android/app/Activity.html#onStop()

Hungry Coder
  • 1,800
  • 17
  • 22
0

I have tried looking into this for a while now. I have come up with a few resources that might help explain the change in the lifecycle. With the new KitKat update, it appears that they introduced some sort of low-end power mangagement as stated in both of these sites here and here.

It isn't specifically stated that this is why you are experiencing the lifecycle change, but IMO if Android now automatically tries to create a "leaner" environment it may be forcing the app to call onStop() when it is freeing up resources.

I know it isn't an exact answer but I hope this helps. I agree that this hasn't always been this way and you are definitely on to something interesting here.

Programmer
  • 459
  • 5
  • 20
0

One more important point to note-

If you change the setting to lock the phone after 10 seconds when you press power button then onStop() will be called after 10 seconds of pressing power button.

Krishna Agarwal
  • 131
  • 2
  • 7
-1

Your activity's on pause will only be called if your activity is still partially visible to the user, otherwise it's stopped.

So the power button has always called onStop() because your activity is actually stopped not just paused.

elmorabea
  • 3,243
  • 1
  • 14
  • 20
  • 1
    No you are wrong. Power button has not always called onStop() in previous Android versions. – Fraggle Jul 23 '14 at 15:31
  • The very definition of onStop() method, is that it's called when your activity is no longer visible or even partially visible to the user. Could you point me to the API level that it called onPause() ? @Fraggle – elmorabea Aug 21 '14 at 07:21
  • @Mahmound I am well aware of the definitions. However, I think the power button / screen off is a special case that has been handled differently over the years. – Fraggle Aug 21 '14 at 13:45
  • So you "think", you don't actually know the info?, you're guessing that it has been handled different over the years ? any Refs to that info please ? – elmorabea Aug 21 '14 at 15:20
  • First, see my recent edits. The issue apparently has to do with the minSDK/targetSDK settings. I've verified that even on KitKat onStop is not called with minSDK/targetSDK of 4/8. What I don't know is what values cause it to change or where that change was documented. I don't know the full history, hence the question. – Fraggle Aug 21 '14 at 15:23