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. JS及Dom练习 | 模态对话框及复选框操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. unity文件 PlayerPrefs.SetInt 保存 And PlayerPrefs.GetInt读取

    unity文件保存读取PlayerPrefs.SetInt   And  PlayerPrefs.GetInt using UnityEngine; using System.Collections; ...

  3. Angular4+NodeJs+MySQL 入门-06 接口配置

    在上篇中说了怎么调用接口,这篇就来说说,接口配置吧. 后端是用NodeJS来写的,由于写后台(以前用的是C#语言)的时候,大部操作都在是对数据库表的增.删.改.查操作, 比如:根据查询出来的数据,然后 ...

  4. Python 中的反射和自省

    本文主要介绍Python中的反射和自省,以及该机制的简单应用 熟悉Java的程序员,一定经常和Class.forName打交道.即使不是经常亲自调用这个方法,但是在很多框架中(spring,eclip ...

  5. [转]how to use both JDK 7 and JDK 8 in one build

    Note: This article is original from https://gist.github.com/aslakknutsen/9648594 JDK 8 Released Most ...

  6. java中创建User Libray

    第一步:右键项目==>Build Path ==>Configure Build Path... 第二步:选择Libraries==>点击 Add Library.. 第三步:选择U ...

  7. 【密码学】RSA算法原理

    RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密. RSA的算法涉及三个参数,n.e1.e2. 其中,n是两个大质数p.q的积,n的二进制 ...

  8. flex buider 4.6 打开设计模式(designer)时提示内存不足错误的解决办法

    先申明,此方法只适用于flex builder 4.6及以下版本, flex builder 4.7以后已经完全取消了designer功能,是没有办法补救的. 1. 首先下载APE文件,这个文件好像是 ...

  9. centos6.5下搭建oracle 11g

    1.安装依赖 sudo yum install binutils compat-libstdc++-33 compat-libstdc++-33.i686 elfutils-libelf elfuti ...

  10. node.js分片上传文件

    前端 : <html> <head> <title>分片上传文件</title> </head> <body> <div ...