Activity中ConfigChanges属性的用法

来源:互联网 发布:写英语论文的软件 编辑:程序博客网 时间:2024/04/25 22:34

程序在运行时,一些设备的配置可能会改变,如:横竖屏的切换、键盘的可用性等,这样的事情一发生,Activity会重新启动,

其中的过程是:在销毁之前会先 called onSaveInstanceState()去保存你应用中的一些数据,然后called onDestroy(),最后才去called onCreate()或 onRestoreInstanceState()方法去重新启动Activity。 当指定的属性发生变化时,不会去重新启动Activity,而是通知程序去调用 onConfigurationChanged()函数 

例如:在进行横竖屏的切换时,会重新启动 Activity,而定义了这个属性,就不会重新启动Activity了,而是去调用 onConfigurationChanged()函数 ConfigChanges是用以监听声明属性变化,作出相应的操作。

具体使用属性如下: “mcc“ The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the MCC.移动国家号码, 由三位数字组成,每个国家都有自己独立的MCC,可以识别手机用户所属国家。 国际移动用户识别码所属国家代号是改变了----- sim被侦测到了,去更新mcc “mnc“ The IMSI mobile network code (MNC) has changed — that is, a SIM hasbeen detected and updated the MNC.移动网号, 在一个国家或者地区中,用于区分手机用户的服务商。 国际移动用户识别码的移动网号码是改变了------ sim被侦测到了,去更新mnc “locale“ The locale has changed — for example, the user has selected a new language that text should be displayed in. 用户所在地区发生变化。用户选择了一个新的语言会显示出来 “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.用户打开手机硬件键盘。键盘的可用性发生了改变 “orientation“ The screen orientation has changed — that is, the user has rotated the device.设备旋转,横向显示和竖向显示模式切换。 

Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations. "screenSize" The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. 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). Added in API level 13. 

以上两条应该是大家用过最多的两个属性。在横竖屏切换的时候,加上这两条属性,只会调用onConfigurationChanged方法,而不会重新走activity生命 周期。 “fontScale“ The font scaling factor has changed — that is, the user has selected a new global font size.

全局字体大小缩放发生改变,选择了不同的全局字体 "uiMode" The user interface mode has changed — this can be caused when the user places the device into a desk/car dock or when the the night mode changes. See UiModeManager. Introduced in API Level 8.

字面意思是用户切换了日间/夜间模式,用户的模式发生了变化 以下几个正常情况下不会发生: “touchscreen“ The touchscreen has changed. (This should never normally happen.) “navigation“ The navigation type has changed. (This should never normally happen.)导航发生了变化 "smallestScreenSize" The physical screen size has changed. This represents a change in size regardless of orientation, so will only change when the actual physical screen size has changed such as switching to an external display. A change to this configuration corresponds to a change in the smallestWidth configuration. 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). Added in API level 13.屏幕的物理大小改变了,如:连接到一个外部的屏幕上

0 0