通过设置ConfigChanges这个属性可以捕捉设备状态变化。
例转屏时,保存所做操作继续进行。具体设置如下:
1 在相关的AndroidMainfest.xml中
<activity
添加android:configChanges="orientation"
2 在相关的.java文件中添加方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
//landscape表示横向
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//portrait表示纵向
}
}
在android中每次屏幕的切换会重启Activity。加上configChanges=“keyboardHidden”属性,就不会重启activity,而是去调用onConfigurationChanged方法。
![]()
ConfigChanges设置方法:将下列字段用“|”分隔开,例:“orientation|keyboardHidden"
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.全局字体大小缩放发生改变
具体见android.content.res.configuration
本文介绍如何在Android应用中使用ConfigChanges属性来监听设备的状态变化,如屏幕旋转等,并提供了具体的实现方法,避免因屏幕旋转导致的Activity重启。
1432

被折叠的 条评论
为什么被折叠?



