最近在帮客户调试一个项目,要求用户按下电源键(锁屏)后调用一个SDK关闭接口。
在正常的manifest文件中我们通常会做类似设置:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />我们来看看按下电源键后Activity的状态变化:
可以看到,按下电源键后,显示执行了onPuase,接着是onStop,然后才收到按下电源键的系统广播。
那么,如果把
android:targetSdkVersion去掉,改成这样:
<uses-sdk
android:minSdkVersion="8"/>会怎样呢,请看如下:
我去,居然不走onStop了!
深层原因还在研究中。
----------------------------------------------------------------------------------------------------------------------
原因找到了:
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.
本文探讨了当应用程序的目标SDK版本不同时,按下电源键后Activity生命周期的变化情况。具体表现为,在目标SDK版本为10及以下时,按下电源键只会触发onPause而不会触发onStop;而在目标SDK版本为11及以上时,则会依次触发onPause和onStop。
466

被折叠的 条评论
为什么被折叠?



