Fraggle Fraggle - 5 months ago 21
Android Question

Power button click now calls onStop in Activity on Android KitKat, previously was just onPause?

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.

Answer Source

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