Android onConfigurationChanged(Configuration cfg) 无法触发问题
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方法
<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();
}
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);
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) 无法触发问题的更多相关文章
- Android onConfigurationChanged的作用
API原文说明: android:configChangesLists configuration changes that the activity will handle itself. When ...
- Android onConfigurationChanged用法(规避横竖屏切换导致的重新调用onCreate方法)
onConfigurationChanged的目的是为了规避横竖屏切换干掉activity而重新调用onCreate方法的问题:有的时候,我们希望重新进入OnCreate生命周期,此时可以调用onSa ...
- android onConfigurationChanged的那点事
Android学习笔记——关于onConfigurationChanged 从事Android开发,免不了会在应用里嵌入一些广告SDK,在嵌入了众多SDK后,发现几乎每个要求在AndroidMan ...
- android onConfigurationChanged讲解
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 相信大家对这个属性已经耳熟能详,如果大家受过转屏的折磨的话! 老规矩,先讲讲官方文档是怎么说的.为什 ...
- Android模拟、实现、触发系统按键事件的方法
Android模拟.实现.触发系统按键事件的方法 /** * 模拟系统按键. * * @param keyCode */ public static void onKeyEvent(final ...
- Android onConfigurationChanged 收不到回调
我返现,90度横屏 旋转到270度横屏onConfigurationChanged 是收不到回掉的.尽管清单里面声明了什么: android:configChanges="orientati ...
- android recycleview 中禁止多点触发
int currentapiVersion = android.os.Build.VERSION.SDK_INT; if (currentapiVersion >= android.os.Bui ...
- [Android Pro] proguard.cfg 配置文件
转载自:http://my.oschina.net/zhangzhihao/blog/72393 # ------------------------------------- # android 原 ...
- Android onConfigurationChanged 不执行
自从Android 3.2(API 13),screen size也开始跟着设备的横竖切换而改变. 所以,在AndroidManifest.xml里设置的MiniSdkVersion和 TargetS ...
随机推荐
- 【Angular】排序
Correct way to integrate Jquery plugins in Angular.js gaurav123337/AngularOtherJqueryPluginDemo 超强的拖 ...
- (转载)遍历memcache中已缓存的key
(转载)http://www.cnblogs.com/ainiaa/archive/2011/03/11/1981108.html 最近需要做一个缓存管理的功能.其中有一个需要模糊匹配memcache ...
- 动态规划(背包问题):POJ 1742 Coins
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 32955 Accepted: 11199 Descripti ...
- [Locked] Longest Substring with At Most Two Distinct Characters
Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...
- Wow! Such Doge! - HDU 4847 (水题)
题目大意:题目描述了一大堆.....然而并没什么用,其实就是让求给的所有字符串里面有多少个"doge",不区分大小写. 代码如下: ====================== ...
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- url找不到指定位置
刚才遇到了把css装入一个文件夹下,然后有的图片失效了. 后来解决是在.css里把./images 换成 ../images 原来css找图片的地址是基于自身.css文件的位置寻找的,不是以引用它的. ...
- objective-c 加号 减号 - +
“加号代表static”是错误的说法,可能跟你那样表达的人其实意思是:“前置加号的方法相当于Java 里面的静态方法”. 在Oc中,方法分为类方法和实例方法. 前置加号(+)的方法为类方法,这类方法是 ...
- android 自定义Toast显示风格
1.创建一个自己想要显示Toast风格的XML如下代码(toast_xml.xml): <?xml version="1.0" encoding="utf-8&qu ...
- 【足迹C++primer】表达式求值
表达式求值 /** * 功能:表达式求值(0到9) * 时间:2014年6月15日08:02:31 * 作者:cutter_point */ #include<stdlib.h> #inc ...