http://blog.csdn.net/xiaodongvtion/article/details/679938 轉載自該文章。
注意:onConfigurationChanged事件並不是只有屏幕方向改變纔可以觸發,其他的一些系統設置改變也可以觸發,比如打開或者隱藏鍵盤。
當我們的屏幕方向發生改變時,就可以觸發onConfigurationChanged事件。我們要想當前的activity捕獲這個事件,需要做以下這麼幾件事情。
第一:權限聲明:
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
API中說該權限允許我們改變配置信息,但是我們再改變屏幕方向的程序中卻並沒有用到該權限,是不是相互衝突了呢?這裏我們可以這樣認爲,當我們聲明該權限的的時候,系統允許我們通過重寫activity中的onConfigurationChanged方法來捕獲和修改某些配置信息。
第二:聲明activity要捕獲的事件類型,
如
<activity
Android:name=".EX05_23"
Android:label="@string/app_name"
Android:configChanges="orientation|keyboard">
<intent-filter>
<action Android:name="android.intent.action.MAIN" />
<category Android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
這裏一定要聲明Android:configChanges屬性,該屬性規定了我們可以在程序中捕獲到的事件類型,多個事件類型用|分隔。
如果這裏沒有orientation,那麼我們再程序中是無法捕獲到屏幕改變的事件的。
第三:
重寫Activity中的onConfigurationChanged方法。
例如:
@Override
public void onConfigurationChanged(Configuration newConfig) {
// 當新設置中,屏幕布局模式爲橫排時
if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
{
//TODO 某些操作
}
super.onConfigurationChanged(newConfig);
}
防止再次調用onCreate,首先,需要設置android:configChanges,可選屬性如下:
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.全局字體大小縮放發生改變 |
然後重載onConfigurationChanged
以下自己收集的整理:
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//橫向
setContentView(R.layout.file_list_landscape);
}else{
//豎向
setContentView(R.layout.file_list);
}
但是,自從Android 3.2(API 13),在設置Activity的android:configChanges="orientation|keyboardHidden"後,還是一樣會重新調用各個生命週期的。
因爲screen size也開始跟着設備的橫豎切換而改變。
所以,在AndroidManifest.xml裏設置的MiniSdkVersion和 TargetSdkVersion屬性大於等於13的情況下,如果你想阻止程序在運行時重新加載Activity,除了設置"orientation"
你還必須設置"ScreenSize"。
解決方法:
AndroidManifest.xml中設置android:configChanges="orientation|screenSize“
禁止橫豎屏切換
android中每次屏幕方向切換時都會重啓Activity,所以應該在Activity銷燬前保存當前活動的狀態,在Activity 再次Create的時候載入配置,那樣,進行中的遊戲就不會自動重啓了
在AndroidManifest.xml的activity(需要禁止轉向的activity)配置中加入 android:screenOrientation=”landscape”屬性即可(landscape是橫向,portrait是縱向)。