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

早过忘川 提交于 2019-12-05 09:18:04

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!