电源按钮点击现在调用Android KitKat上的Activity中的onStop,之前只是onPause?(Power button click now calls onStop in Activ

编程入门 行业动态 更新时间:2024-10-24 04:52:17
电源按钮点击现在调用Android KitKat上的Activity中的onStop,之前只是onPause?(Power button click now calls onStop in Activity on Android KitKat, previously was just onPause?)

所以我刚刚意识到当单击电源按钮关闭屏幕时,onStop会在我的Activity中被调用。 以前只调用onPause。 这是KitKat的变化,是否有任何关于它的说明(是故意的)吗? 这是KitKat的变化还是特定制造商实施的?

编辑:我将很快更新这个更多信息 。 我认为这种变化比我最初意识到的更微妙,可能是由于我持有部分唤醒锁或听取GPS更新。 无论如何,我所知道的是,在我的代码中,在KitKat之前,单击电源按钮时未调用onStop。 也许这也取决于设备。

编辑:新信息。 通过以下设置,单击电源按钮时不会调用onStop():Android minSDKVersion = 4,targetSDKVersion = 8,(如果使用Android Studio,则设置compileSdkVersion = 8)。 在2台设备上验证了这一点(运行KitKat和JellyBean)所以这个问题不是原始中首次提到的KitKat,而是最小的目标sdk设置。 Bounty将被授予任何人可以找到更改时的参考,或者至少显示第一个最小/目标sdk设置,该设置更改了从电源按钮单击调用onStop的行为。

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.

最满意答案

生命周期的变化来自Honeycomb,或者将针对sdk 10或更低的应用程序更改为针对sdk 11或更高版本的应用程序。

如果Android项目声明targetSDKversion为10或更小,那么当单击电源按钮时,将调用onPause, 但不会调用onStop 。 这与许多人的想法相反。 但是,当targetSDKversion = 11或更高时, onPause将在单击电源按钮时调用onStop

应该注意的是,在某些文档中,它声明onStop将“当活动不再对用户可见时被调用, 因为另一个活动已经恢复并且正在覆盖这个活动。”(重点补充)但在许多其他它只是声明只要当前活动不再可见,就会调用onStop。 因此,在SDK 11之前,也许电源按钮单击是故意调用onPause,因为没有其他活动覆盖当前的活动。 屏幕刚刚关闭。 然后使用Honeycomb,他们改变了实现以匹配其他解释(不再可见)。

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

更多推荐

本文发布于:2023-08-07 21:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   电源   KitKat   Activity   onStop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!