http://hi.baidu.com/yangduoliver/blog/item/0702463424573b55251f14f2.html
在以前的版本中只要在AndroidManifest.xml文件中對activity指定android:configChanges="keyboardHidden|orientation"屬性,轉屏的時候就會不再重新調用OnCreate()函數,而是調用onConfigurationChanged()。
但是在自從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" /> |