1. 程式人生 > >Android 設定和不設定 android:targetSdkVersion的區別

Android 設定和不設定 android:targetSdkVersion的區別

最近在幫客戶除錯一個專案,要求使用者按下電源鍵(鎖屏)後呼叫一個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.