[Android] Activity ConfigChanges屬性

經過設置這個屬性可使Activity捕捉設備狀態變化,如下是能夠被識別的內容: 
設置方法:將下列字段用「|」符號分隔開,例如:「locale|navigation|orientation

java

Value Description
mcc The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the MCC.移動國家號碼,由三位數字組成,每一個國家都有本身獨立的MCC,能夠識別手機用戶所屬國家。
mnc The IMSI mobile network code (MNC) has changed — that is, a SIM hasbeen detected and updated the MNC.移動網號,在一個國家或者地區中,用於區分手機用戶的服務商。
locale The locale has changed — for example, the user has selected a new language that text should be displayed in.用戶所在地區發生變化。
touchscreen The touchscreen has changed. (This should never normally happen.)
keyboard The keyboard type has changed — for example, the user has plugged in an external keyboard.鍵盤模式發生變化,例如:用戶接入外部鍵盤輸入
keyboardHidden The keyboard accessibility has changed — for example, the user has slid the keyboard out to expose it.用戶打開手機硬件鍵盤
navigation The navigation type has changed. (This should never normally happen.)
orientation The screen orientation has changed — that is, the user has rotated the device.設備旋轉,橫向顯示和豎向顯示模式切換。
fontScale The font scaling factor has changed — that is, the user has selected a new global font size.全局字體大小縮放發生改變

經過一個例子介紹這個屬性的用法: 首先須要修改項目的manifest:
android

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidres.ConfigChangedTesting"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ConfigChangedTesting"
                  android:label="@string/app_name"
                  android:configChanges="keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

在Activity中添加了 android:configChanges屬性,目的是當所指定屬性(Configuration Changes)發生改變時,通知程序調用 onConfigurationChanged()函數。面試


橫豎屏切換時候activity的生命週期 android:configChanges

一、不設置Activity的android:configChanges時,切屏會從新調用各個生命週期,切橫屏時會執行一次,切豎屏時會執行兩次app

二、設置Activity的android:configChanges="orientation"時,切屏仍是會從新調用各個生命週期,切橫、豎屏時只會執行一次函數

三、設置Activity的android:configChanges="orientation|keyboardHidden"時,切屏不會從新調用各個生命週期,只會執行onConfigurationChanged方法字體

今天遇到一個面試題,讓寫出橫屏切換豎屏Activity的生命週期。之前好像看到過,當時沒用,因而沒注意,結果今天有這個題。spa

網上查了下,總結下:code

一、新建一個Activity,並把各個生命週期打印出來orm

二、運行Activity,獲得以下信息xml

onCreate-->

onStart-->

onResume-->

三、按crtl+f12切換成橫屏時

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

四、再按crtl+f12切換成豎屏時,發現打印了兩次相同的log

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

五、修改AndroidManifest.xml,把該Activity添加 android:configChanges="orientation",執行步驟3

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

六、再執行步驟4,發現不會再打印相同信息,但多打印了一行onConfigChanged

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

onConfigurationChanged-->

七、把步驟5的android:configChanges="orientation" 改爲 android:configChanges="orientation|keyboardHidden",執行步驟3,就只打印onConfigChanged

onConfigurationChanged-->

八、執行步驟4

onConfigurationChanged-->

onConfigurationChanged-->

總結:

一、不設置Activity的android:configChanges時,切屏會從新調用各個生命週期,切橫屏時會執行一次,切豎屏時會執行兩次

二、設置Activity的android:configChanges="orientation"時,切屏仍是會從新調用各個生命週期,切橫、豎屏時只會執行一次

三、設置Activity的android:configChanges="orientation|keyboardHidden"時,切屏不會從新調用各個生命週期,只會執行onConfigurationChanged方法

補充一點,當前Activity產生事件彈出Toast和AlertDialog的時候Activity的生命週期不會有改變

Activity運行時按下HOME鍵(跟被徹底覆蓋是同樣的):onSaveInstanceState --> onPause --> onStop      onRestart -->onStart--->onResume

相關文章
相關標籤/搜索