现在单击电源按钮可在Android KitKat的Activity中调用onStop,以前只是onPause? [英] Power button click now calls onStop in Activity on Android KitKat, previously was just onPause?

查看:84
本文介绍了现在单击电源按钮可在Android KitKat的Activity中调用onStop,以前只是onPause?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我刚刚意识到,单击电源按钮以关闭屏幕时,onStop在我的活动中被调用.以前只调用了onPause.这是KitKat的变更,是否在任何地方都有说明(是否有意)?这是KitKat的变更还是特定制造商实施的变更?

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?

我将在不久的将来使用更多信息进行更新.我认为更改比我最初意识到的要微妙得多,这可能是由于我持有部分唤醒锁或正在收听GPS更新所致.无论如何,我所知道的是,在我的代码中,在KitKat之前,单击电源按钮时未调用onStop.也许这也取决于设备.

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.

新信息.使用以下设置,单击电源按钮时不会调用onStop():Android minSDKVersion = 4和targetSDKVersion = 8,(如果使用Android Studio,请同时设置compileSdkVersion = 8).在2个设备(运行KitKat和JellyBean)上对此进行了验证.因此,此问题不是最初提到的KitKat,而是最小目标sdk设置. 赏金将被授予,只要该人可以找到更改时的引用,或者至少显示第一个最小/目标sdk设置即可更改行为,从而通过单击电源按钮单击来调用onStop.

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.

推荐答案

Honeycomb或将面向sdk 10或更低版本的应用程序更改为面向sdk 11或更高版本的应用程序时,生命周期发生了变化.

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.

如果Android项目声明targetSDKversion为10或更小,则单击电源按钮时,将调用onPause,但不调用onStop .这与许多人的想法相反.如果targetSDKversion = 11或更高,则单击电源按钮时,将依次调用 onPause然后onStop .

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.

应注意,在某些文档中,它声明onStop将在用户不再可见该活动时被调用,因为已恢复另一活动并涵盖了该活动. "(添加了重点),但在许多其他地方,它只是声明只要当前活动不再可见,就会调用onStop.因此,在SDK 11之前,电源按钮的单击可能是故意调用onPause的,因为没有其他活动覆盖当前的活动.屏幕只是关闭了.然后,他们使用Honeycomb更改了实现,以匹配其他解释(不再可见).

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

这篇关于现在单击电源按钮可在Android KitKat的Activity中调用onStop,以前只是onPause?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆