横竖屏切换时候的生命周期以及configchanges介绍
2016-11-22 15:19
435 查看
一、横竖屏切换时的生命周期执行顺序
启动activity时的执行:onCreate---onStart---onResume横竖屏切换时:onPause()----onSaveInstanceState()----onStop()----onDestroy()----onCreate()-----onStart()-----onRestoreInstanceState()----onResume()
可以看出,每次都是先销毁再创建,如何让其不是每一次都是创建一个新的呢,接下来就来看看configchanges属性。该属性可以设置多个值,用"|"隔开,例如:“locale|navigation|orientation。设置了android:configChanges属性后,当指定的属性发生变化时,不会去重新启动Activity,而是通知程序去调用Activity的onConfigurationChanged()方法。
二、configchanges的属性的介绍
再来看看configchanges都有哪些属性值:mcc:The IMSI mobile country code (MCC) has changed — a SIM has been detected and updated the MCC.
IMSI(国际移动用户识别码)发生改变,检测到SIM卡,或者更新MCC
mnc:The IMSI mobile network code (MNC) has changed — a SIM has been detected and updated the MNC.
IMSI网络发生改变,检测到SIM卡,或者更新MCC
其中mcc和mnc理论上不可能发生变化
locale:The locale has changed — 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 revealed the hardware keyboard.
键盘发生改变,例如,用户使用了硬件键盘
navigation:The navigation type (trackball/dpad) has changed. (This should never normally happen.)
导航发生改变,(这通常不应该发生) 举例:连接蓝牙键盘,连接后确实导致了navigation的类型发生变化。因为连接蓝牙键盘后,我可以使用方向键来navigate了
screenLayout:The screen layout has changed — this might be caused by a different display being activated.
屏幕的布局发生改变,这可能导致激活不同的显示
fontScale:The font scaling factor has changed — the user has selected a new global font size.
全局字体大小缩放发生改变
orientation:The screen orientation has changed — that is, the user has rotated the device.设备旋转,横向显示和竖向显示模式切换。
screenSize: 屏幕大小改变了
smallestScreenSize: 屏幕的物理大小改变了,如:连接到一个外部的屏幕上
三、关于configchanges举例说明
简言之,在Activity中添加了android:configChanges属性,目的是当android:configChanges所设置的属性值对应的配置属性发生改变时,通知程序调用 onConfigurationChanged()函数,而不是重启Activity。比如上边说的横竖屏切换,当在mainfest中配置了
android:configChanges="orientation|screenSize|keyboardHidden"
之后,横竖屏切换时只会执行onConfigurationChanged()方法而不会重启activity
四、onSaveInstanceState()和onRestoreInstanceState()的触发执行
官网上的介绍是:Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key)也就是说当某个activity变得容易被系统销毁时,该activity的onSaveInstanceState就会被执行,除非该activity是被用户主动销毁的,例如当用户按BACK键的时候。什么是容易被系统销毁,那就是还没有销毁,而是一种可能性,那么这种可能性有哪些呢?当一个activity在栈顶显示时,哪些操作能够触发onSaveInstanceState()的执行呢?
1、上边已经提到的横竖屏的切换
2、Home键返回桌面的时候执行时:onPause()----onSaveInstanceState()----onStop()
3、直接后台切换到其他应用
4、直接锁屏的时候
所以,从上边的可能性可以看出,onSaveInstanceState()的调用遵循一个重要原则,即当系统“未经你许可销毁了你的activity,而不是你自己手动销毁的,这时候onSaveInstanceState会被系统调用,这是系统的责任,因为它必须要提供一个机会让你保存你的数据。
而onRestoreInstanceState被调用的前提是,activity A“确实”被系统销毁了,而如果仅仅是停留在有这种可能性的情况下,则该方法不会被调用,例如,当正在显示activityA时,这时候直接按下电源键锁屏,那么会执行onSaveInstanceState(),紧接着再打开屏幕,这时候activityA不会被系统销毁,所以不会执行onRestoreInstanceState()。
所以这里就需要注意了:onSaveInstanceState()和onRestoreInstanceState()并不一定会成对调用
相关文章推荐
- 【亲测】Activity中的 ConfigChanges 属性以及横竖屏切换时候 Activity 的生命周期
- 横竖屏切换时候activity的生命周期 android:configChanges
- Android--横竖屏切换时候activity的生命周期
- 横竖屏切换时候Activity的生命周期
- 横竖屏切换时候Activity的生命周期
- 横竖屏切换时候Activity的生命周期
- 横竖屏切换时候Activity的生命周期变化
- 横竖屏切换时候Activity的生命周期
- 横竖屏切换时候Activity的生命周期
- Android横竖屏切换时候的activity的生命周期
- 横竖屏切换时候Activity的生命周期
- 横竖屏切换时候Activity的生命周期
- Android系统横竖屏切换时候Activity的生命周期
- Android重写onConfigurationChanged规避横竖屏切换时候重新进入onCreate生命周期
- 横竖屏切换时候Activity的生命周期
- 横竖屏切换时候Activity的生命周期
- 横竖屏切换时候Activity的生命周期
- Fragment和Activity生命周期以及横竖屏切换对生命周期的影响
- 横竖屏切换时候Activity的生命周期
- 横竖屏切换时候Activity的生命周期的总结