但是在自從android3.2以後,再這樣設置的話,會發現轉屏後仍然會調用OnCreate(),而不是onConfigurationChanged();跟蹤framework層代碼,就會發現問題所在,是由於google在android3.2中添加了screensize改變的通知,在轉屏的時候,不僅是orientation發生了改變,screensize同樣也發生了改變,而在判斷是調用onConfigurationChanged還是OnCreate時,採用的是如下判斷:
int diff = activity.mCurrentConfig.diff(config);
if (diff != 0) {
// If this activity doesn't handle any of the config changes then don't bother calling onConfigurationChanged as we'regoing to destroy it.
if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
shouldChangeConfig = true;
}
}
public int getRealConfigChanged() {
return applicationInfo.targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB_MR2 ? (configChanges | ActivityInfo.CONFIG_SCREEN_SIZE
| ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) : configChanges;
}
通過上面的分析,可發現有兩種方法解決該問題:(只需要修改AndroidManifest.xml)
1.指定android:configChanges="keyboardHidden|orientation|screenSize",其他的代碼和以前的代碼一樣處理;
2.在AndroidManifest.xml中指定targetSdkVersion爲3.2以前的版本(3.2的版本號爲13),系統會自動加上screenSize屬性值。
比如:<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="12" />