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. git 拉新项目

                   

  2. zabbix 报警程序

    一,报警程序 本次使用的为onealert http://c.onealert.com/console/ucid/login.jsp 二,服务端安转 下面为他教的怎么安装这个东西 第一步: 找到脚本目 ...

  3. xlua的自定义加载

    具体可以先看xlua的自定义加载的demo,那个用lamda表达式做的 我这个更好理解 主要是ReadFile2的结构问题,必须的写成这样

  4. CF 304B——Calendar——————【年月日计算】

    B - Calendar Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  5. jqGrid随窗口大小变化自适应宽度

    $(function(){ $(window).resize(function(){ $("#jqgridID").setGridWidth($(window).width()); ...

  6. UML建模—EA的使用起步

    Enterprise Architect(EA) 是一个功能比较强悍的建模工具. 对于一个软件设计者来说,从需求分析到业务设计.类模型设计.数据库设计到测试.发布.部署等一系列软件设计必须的操作都可以 ...

  7. C# 使用cookie实现登录

    首先,我们需要做的是什么? 我们成功登录之后,跳转到主界面,然后主界面的登录按钮变成头像啥的.下一次打开网页就要判断有没有登录过,有cookie就不需要登录,直接显示头像 1.成功登录后,客户端请求服 ...

  8. 系统更新后vs2012无法打开方案资源管理器

    系统更新后vs2012无法打开方案资源管理器 vs调试报错: 未找到与约束 ContractName Microsoft.VisualStudio.Language.Intellisense.IGly ...

  9. 比较详细的mysql的几种连接功能分析,只要你看完就能学会的好东西

    下面是例子分析表A记录如下: aID        aNum 1           a20050111 2           a20050112 3           a20050113 4   ...

  10. spring集成JPA的三种方法配置

    JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 spring提供三种方法集成JPA:1.L ...