星空网 > 软件开发 > 操作系统

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

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

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

在注意:

   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="12" /> 

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(this.getCurrentFocus().getWindowToken(), 0);


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

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


 
 
 

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

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊推新选品计划,仓储、配送、退货通通免费!:https://www.ikjzd.com/articles/112947
亚马逊站内广告的系统和逻辑化的运营思维:https://www.ikjzd.com/articles/112948
稳居中部第一!河南省前11月外贸进出口总值超5000亿元!:https://www.ikjzd.com/articles/112949
新手卖家不要掉进亚马逊站外引流的这些误区:https://www.ikjzd.com/articles/112956
2020年亚马逊新品推广,卖家必须掌握这5大实用方法!:https://www.ikjzd.com/articles/112957
一票405元!香港DHL新增低申报附加费!:https://www.ikjzd.com/articles/112958
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流