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.
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).
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()
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.
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.
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.
来源:https://stackoverflow.com/questions/24887560/power-button-click-now-calls-onstop-in-activity-on-android-kitkat-previously-wa