Activity 的 ConfigChanges 屬性
官方解釋: 通過設定這個屬性可以使Activity捕捉裝置狀態變化,以下是可以被識別的內容:
CONFIG_FONT_SCALE
CONFIG_MCC
CONFIG_MNC
CONFIG_LOCALE
CONFIG_TOUCHSCREEN
CONFIG_KEYBOARD
CONFIG_NAVIGATION
CONFIG_ORIENTATION
設定方法:將下列欄位用“|”符號分隔開,例如:“locale|navigation|orientation”
Value
Description
“mcc“
The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the 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.全域性字型大小縮放發生改變
專案的AndroidManifest.xml檔案配置方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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 的生命週期
測試程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
public class AndroidLifecycle extends Activity { private String TAG = "AndroidLifecycle"; public void onCreate(Bundle savedInstanceState) { Log.i(TAG,"AndroidLifecycle =======onCreate()========"); super.onCreate(savedInstanceState); setContentView(R.layout.main); }
@Override protected void onSaveInstanceState(Bundle outState) { Log.i(TAG,"AndroidLifecycle =======onSaveInstanceState()========"); super.onSaveInstanceState(outState); }
@Override protected void onRestoreInstanceState(Bundle outState) { Log.i(TAG,"AndroidLifecycle=======onRestoreInstanceState()========"); super.onRestoreInstanceState(outState); }
@Override public void onConfigurationChanged(Configuration newConfig) { Log.i(TAG,"AndroidLifecycle =======onConfigurationChanged()========"); super.onConfigurationChanged(newConfig); }
// Called after onCreate — or after onRestart when the activity had been // stopped, but is now again being displayed to the user. It will be // followed by onResume protected void onStart() { Log.i(TAG,"AndroidLifecycle =======onStart()========"); super.onStart(); }
// Called after onRestoreInstanceState, onRestart, or onPause, for your // activity to start interacting with the user protected void onResume() { Log.i(TAG,"AndroidLifecycle =======onResume()========"); super.onResume(); }
// Called as part of the activity lifecycle when an activity is going into // the background, but has not (yet) been killed protected void onPause() { Log.i(TAG,"AndroidLifecycle =======onPause()========"); super.onPause(); }
// Called when you are no longer visible to the user. You will next receive // either onRestart, onDestroy, or nothing, depending on later user // activity. protected void onStop() { Log.i(TAG,"AndroidLifecycle =======onStop()========"); super.onStop(); }
// Perform any final cleanup before an activity is destroyed protected void onDestroy() { Log.i(TAG,"AndroidLifecycle =======onDestroy()========"); super.onDestroy(); }
// Called after onStop when the current activity is being re-displayed to // the user (the user has navigated back to it). It will be followed by // onStart and then onResume protected void onRestart() { Log.i(TAG,"AndroidLifecycle =======onRestart()========"); super.onRestart(); } } |
Activity屬性配置:
1 2 3 4 5 6 7 8 9 10 11 |
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".AndroidLifecycle" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
</application> |
在 Android 2.2 模擬器上測試結果為:
1、不設定Activity的android:configChanges時:
Simulator_切換到橫屏
Simulator_切換到豎屏
模擬器這種情況切屏會重新呼叫各個生命週期,切橫屏時會生命週期執行一次,切豎屏時生命週期會執行兩次,且橫豎屏onConfigurationChanged未被呼叫。但是在HTC DESIRE HD(G10) 這款手機上測試結果為:生命週期都只執行一次,且橫豎屏onConfigurationChanged未被呼叫。
2、設定Activity的android:configChanges="orientation"時:
Simulator_orientation_切換到橫屏
Simulator_orientation_切換到豎屏
模擬器這種情況切屏還是會重新呼叫各個生命週期,切橫屏onConfigurationChanged未執行、豎屏時onConfigurationChanged執行1次。但是在HTC DESIRE HD(G10) 這款手機上測試結果為:生命週期未發生變化,且橫豎屏onConfigurationChanged都只執行一次。
3、設定Activity的android:configChanges="orientation|keyboardHidden"時:
Simulator_orientation_keyboardHidden_切換到橫屏
Simulator_orientation_keyboardHidden_切換到豎屏
模擬器這種情況切屏不會重新呼叫各個生命週期,切橫屏時onConfigurationChanged會執行1次,切豎屏時onConfigurationChanged方法呼叫2次。 但是在HTC DESIRE HD(G10) 這款手機上測試結果為:橫豎屏onConfigurationChanged都只執行一次。
結論:可以看出在G10這款真機上測試的結果比模擬器更加合理。