在以前的版本中只要在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" />





建议使用第一种方法。



onConfigurationChanged not getting called


Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).


(From http://developer.android.com/guide/topics/resources/runtime-changes.html)


http://stackoverflow.com/questions/5620033/onconfigurationchanged-not-getting-called

更多相关文章

  1. android OSChina 客户端源代码剖析
  2. android代码混淆 GSON完满解决
  3. Android开源代码解读の地图照片应用Panoramio的实现详解(六)
  4. 在app引擎端点的启动时执行代码
  5. javaScript函数中执行C#代码中的函数
  6. 反编译APK 得到JAVA代码和资源文件源码
  7. java.io.File vs java.nio.Files这是新代码中的首选?
  8. 拖动层的javasvript代码 十行代码即可写出兼容版拖动层
  9. 我无法让这个简单的ajax代码工作

随机推荐

  1. XMLTextReader和XmlDocument读取XML文件
  2. XML和XSLT结合使网站设计浑然一体
  3. 对于任意的XML的遍历
  4. 如何使用XML实现多渠道接入网站的构架
  5. 灵活调用xsl来解析xml文档(js异步)
  6. FireFox对XML的处理兼容IE的节点处理方法
  7. 读写xml所有节点个人小结 和 读取xml节点
  8. 基于关系型数据库引擎的"XML"索引技术
  9. XML 增、删、改和查示例
  10. 效率最高的xml解析方式