1.android:configChanges="orientation|keyboardHidden"的使用 

当在activity加上android:configChanges="keyboardHidden|orientation"属性,就不会重启activity.而只是调用onConfigurationChanged(Configuration newConfig).这样就可以在这个方法里调整显示方式.

在xml文件里面可以进行配置configChanges也可以在代码中动态配置

注意:

1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次
   2、设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次
   3、设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法

 但是:
1)在版本android:targetSdkVersion<=12屏幕切换的时候才会触发调用onConfigurationChanged(Configuration newConfig)此方法
 

<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="" />

2)在版本13+以上的时候必须添加上"|screenSize",即 android:configChanges="orientation|keyboardHidden|screenSize" 才会触发onConfigurationChanged(Configuration newConfig)此方法

<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="13" />

2.下面是屏幕切换的代码:

Configuration cfg=getResources().getConfiguration();

if(cfg.orientation==Configuration.ORIENTATION_LANDSCAPE){
GetSystemInfoActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}
if(cfg.orientation==Configuration.ORIENTATION_PORTRAIT){
GetSystemInfoActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

重写chonConfigurationChanged(Configuration newConfig)方法触发调用

@Override
public void onConfigurationChanged(Configuration cfg) {
super.onConfigurationChanged(cfg);

Toast.makeText(GetSystemInfoActivity.this, "test:"+(cfg.orientation==Configuration.ORIENTATION_LANDSCAPE?"横屏":"竖屏"),Toast.LENGTH_SHORT).show();

}

 3.屏幕切换时关闭软键盘输入法弹出
 <activity  ...
android:windowSoftInputMode="adjustUnspecified|stateHidden" /> 
 
或者在代码里面调用关闭输入法软键盘的代码:

InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
mInputMethodManager.hideSoftInputFromWindow(acivity.getCurrentFocus().getWindowToken(), 0);

 附上(打开输入法代码):

InputMethodManager inputManager =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);

 4.一进入关闭软键盘:根布局上添加下面两行

android:focusable="true"
android:focusableInTouchMode="true"
 
 

onConfigurationChanged为何不被调用?

英文原文如下:

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)

TL;DR: add "|screenSize" to configChanges when targeting API level 13+

参考:

   1.http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1106/516.html

   2.http://www.cnblogs.com/xiaokang088/p/3540189.html

   3.http://blog.csdn.net/songshimvp1/article/details/50109879

   4.http://www.cnblogs.com/androidez/archive/2013/04/09/3011399.html(关闭输入法参考地址)

Android onConfigurationChanged(Configuration cfg) 无法触发问题的更多相关文章

  1. Android onConfigurationChanged的作用

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

  2. Android onConfigurationChanged用法(规避横竖屏切换导致的重新调用onCreate方法)

    onConfigurationChanged的目的是为了规避横竖屏切换干掉activity而重新调用onCreate方法的问题:有的时候,我们希望重新进入OnCreate生命周期,此时可以调用onSa ...

  3. android onConfigurationChanged的那点事

    Android学习笔记——关于onConfigurationChanged   从事Android开发,免不了会在应用里嵌入一些广告SDK,在嵌入了众多SDK后,发现几乎每个要求在AndroidMan ...

  4. android onConfigurationChanged讲解

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

  5. Android模拟、实现、触发系统按键事件的方法

     Android模拟.实现.触发系统按键事件的方法 /** * 模拟系统按键. * * @param keyCode */ public static void onKeyEvent(final ...

  6. Android onConfigurationChanged 收不到回调

    我返现,90度横屏 旋转到270度横屏onConfigurationChanged 是收不到回掉的.尽管清单里面声明了什么: android:configChanges="orientati ...

  7. android recycleview 中禁止多点触发

    int currentapiVersion = android.os.Build.VERSION.SDK_INT; if (currentapiVersion >= android.os.Bui ...

  8. [Android Pro] proguard.cfg 配置文件

    转载自:http://my.oschina.net/zhangzhihao/blog/72393 # ------------------------------------- # android 原 ...

  9. Android onConfigurationChanged 不执行

    自从Android 3.2(API 13),screen size也开始跟着设备的横竖切换而改变. 所以,在AndroidManifest.xml里设置的MiniSdkVersion和 TargetS ...

随机推荐

  1. WebApp 中用 hashchange 做路由解析

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 从linux内核中学到的编程技巧 【转】

     从linux内核中学到的编程技巧  分类: LINUX 1构建泛型宏 (./linux/include/linux/kernel.h) #define min(x, y) ({ \ typeof(x ...

  3. 获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName

    http://blog.csdn.net/bichenggui/article/details/4774457 -------------------------------------------- ...

  4. SORT ORDER BY STOPKEY

    select * from ( select * from ( select a.*,rownum rn from page a where object_id >1000 and owner= ...

  5. 【转】通知 Toast详细用法(显示view)

    原文网址:http://www.pocketdigi.com/20100904/87.html 今天学习Android通知 Toast的用法,Toast在手机屏幕上向用户显示一条信息,一段时间后信息会 ...

  6. 图解linux下top命令的使用

    top命令经常用来监控linux的系统状况,比如cpu.内存的使用,程序员基本都知道这个命令,但比较奇怪的是能用好它的人却很少,例如top监控视图中内存数值的含义就有不少的曲解. 本文通过一个运行中的 ...

  7. firefox HackBar组件模拟请求POST请求

    组件下载地址:https://addons.mozilla.org/zh-CN/firefox/addon/hackbar/

  8. NOI题库1159 Maze

    1159:Maze 总时间限制: 2000ms 内存限制: 65536kB 描述 Acm, a treasure-explorer, is exploring again. This time he ...

  9. javax.el.PropertyNotFoundException错误

    在J2EE项目的开发过程中,遇到了这个问题,报错如下: 错误原因为在我model里的Person类里定义了一个Name属性,但是读取属性的getter方法的,一般按照属性首字母小写来处理,所以把Nam ...

  10. 病毒侵袭 - HDU 2896(AC自动机)

    分析:有点需要注意的,输入的字符是所有可见的ASCII码,刚开始没看清一直以为是小写字母.............注意到这点后这题就是裸的自动机了.   代码如下: ================= ...