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.全局字体大小缩放发生改变

这里关于屏幕旋转不需要再次调用onCreate是应该设置android:configChanges="orientation|keyboardHidden"
然后重载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是纵向)。

from:http://blog.csdn.net/chencehnggq/article/details/23736869

【转】onConfigurationChanged的更多相关文章

  1. Android 中onConfigurationChanged问题

    onConfigurationChanged 不生效问题解决方案: 1).首先,需要重写onConfigurationChanged函数 @Override    public void onConf ...

  2. android 连接蓝牙扫码枪,程序崩溃之onConfigurationChanged

    当android手机通过蓝牙连接扫码枪时,程序崩溃的原因之一是:键盘弹出或隐藏,触发程序走了onDestory->onCreate的生命周期,从而可能使得页面的某些初始化数据被清除了. 解决方法 ...

  3. android onConfigurationChanged讲解

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 相信大家对这个属性已经耳熟能详,如果大家受过转屏的折磨的话! 老规矩,先讲讲官方文档是怎么说的.为什 ...

  4. onConfigurationChanged is not called&& 翻转屏幕不执行onConfigurationChanged方法&&onConfigurationChanged不执行

    我总结出一句话: 如果target sdk>=13,必须使用如下方式声明activity:android:configChanges="orientation|screenSize&q ...

  5. onConfigurationChanged与OnCreate,究竟谁被调用的问题

    在以前的版本中只要在AndroidManifest.xml文件中对activity指定android:configChanges="keyboardHidden|orientation&qu ...

  6. Android onConfigurationChanged(Configuration cfg) 无法触发问题

     1.android:configChanges="orientation|keyboardHidden"的使用  当在activity加上android:configChange ...

  7. 转:onConfigurationChanged的作用

    API原文说明:android:configChangesLists configuration changes that the activity will handle itself. When ...

  8. Android onConfigurationChanged的作用

    API原文说明: android:configChangesLists configuration changes that the activity will handle itself. When ...

  9. 响应的系统设置的事件——重写onConfigurationChanged响应系统设置更改

    如果程序需要监听系统设置的更改,则可以考虑重写Activity的onConfigurationChanged(Configuration newConfig)方法,该方法是一个基于回调的事件处理方法: ...

  10. onConfigurationChanged方法的使用

    在日常生活中,手机会有很多种配置放生改变的情况,当然,有些时候需要监听他们并对他们进行处理,这就涉及到了onConfiguration方法的使用,我大致说一下,这个方法需要发生在屏幕切换横竖屏,或者选 ...

随机推荐

  1. Xtts v4变化&先决条件&已知问题

    V4变化的主要有:     1.这个采购使用简化的命令.源的一个命令(--backup)和目标的一个命令(--restore). 2.此过程只需要在源和目标的$ TMPDIR(res.txt)之间复制 ...

  2. PIXI 太空玉兔游戏(6)

    想法来源  出于练习看到这篇文章   没有什么难度  效果如下,接下来会用pixijs讲解如何实现 创建应用及舞台 HTML部分只创建标签引入 pixi.min.js  即可: <script ...

  3. Lakeshore

    用来做 html5 特效,Egret游戏引擎 为什么用Egret开发的游戏在某些Android设备上特别卡? { 在 Android 早期版本( 4.4 之前) ,Android WebView 并不 ...

  4. Oracle RAC集群删除节点

    一,节点环境 [root@node1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost ...

  5. php中的$_GET如何获取带有“#”的参数

    <?php echo $_GET['key']; ?> 当url为http://test.com/c.php?key=999时,正常输出:999 当url为http://test.com/ ...

  6. [转]Load ASP.NET MVC Partial Views Dynamically Using jQuery

    本文转自:http://www.binaryintellect.net/articles/218ca630-ba50-48fe-af6e-6f754b5894aa.aspx Most of the t ...

  7. J2EE课程设计:在线书店管理系统

    1.系统实现 使用SpringMVC框架进行开发 使用Maven进行系统构建 使用MySql数据库 项目只实现了查看图书,搜索图书,加入购物车,创建订单,图书管理等基本功能 前台使用Bootstrap ...

  8. mybatis一对多映射

    场景: A:SecControlRulePojo.java B:SecControlSubRulePojo C:SecControlSubRuleManyPojo 实体A中包含List<B> ...

  9. python的元组及其书写规矩

    1.元组 (1)元组看起来犹如列表,但使用圆括号而不是方括号来标识.定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样. (2)元组的元素不可修改,但是可以赋值. 2.规矩 (1)缩进:建议 ...

  10. 科学计算基础包——Numpy

    一.NumPy简介 NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础. 1.NumPy的主要功能 (1)ndarray:一个多维数组结构,高效且节省空间. (2)无需 ...