title: Activity 在横竖屏切换情况下的生命周期变化

date: 2018-04-26 23:05:57

tags: [Activity]

categories: [Mobile,Android]

概述

Activity 在横竖屏切换的时候,生命周期是不一样的,本地通过打印 log 的方式,看下区别.测试的机器是 Android6.0 .

不做任何配置的情况下

第一次启动

D/LifeCircleActivity: onCreate() called with: savedInstanceState = [null]Activity对象的地址:cn.steve.activitylifecycle.LifeCircleActivity@de950fc
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onResume() called

第一次切换成横屏


D/LifeCircleActivity: onPause() called
D/LifeCircleActivity: onSaveInstanceState() called with: outState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]
D/LifeCircleActivity: onStop() called
D/LifeCircleActivity: onDestroy() called
D/LifeCircleActivity: onCreate() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]Activity对象的地址:cn.steve.activitylifecycle.LifeCircleActivity@266fbfb
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onRestoreInstanceState() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]
D/LifeCircleActivity: onResume() called

再切换成竖屏

D/LifeCircleActivity: onPause() called
D/LifeCircleActivity: onSaveInstanceState() called with: outState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]
D/LifeCircleActivity: onStop() called
D/LifeCircleActivity: onDestroy() called
D/LifeCircleActivity: onCreate() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]Activity对象的地址:cn.steve.activitylifecycle.LifeCircleActivity@7e6e82e
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onRestoreInstanceState() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]
D/LifeCircleActivity: onResume() called

小结

默认情况下,每次旋转屏幕都会销毁当前的Activity对象,同时调用 onSaveInstanceState 方法,保存当前的界面状态;之后重新创建 Activity对象, onCreate 参数不为空,回调 onRestoreInstanceState 方法进行恢复。

配置 configChanges="orientation"

第一次启动

D/LifeCircleActivity: onCreate() called with: savedInstanceState = [null]Activity对象的地址:cn.steve.activitylifecycle.LifeCircleActivity@de950fc
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onResume() called

第一次切换成横屏

D/LifeCircleActivity: onPause() called
D/LifeCircleActivity: onSaveInstanceState() called with: outState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]
D/LifeCircleActivity: onStop() called
D/LifeCircleActivity: onDestroy() called
D/LifeCircleActivity: onCreate() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]Activity对象的地址:cn.steve.activitylifecycle.LifeCircleActivity@266fbfb
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onRestoreInstanceState() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]
D/LifeCircleActivity: onResume() called

再切换成竖屏

D/LifeCircleActivity: onPause() called
D/LifeCircleActivity: onSaveInstanceState() called with: outState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]
D/LifeCircleActivity: onStop() called
D/LifeCircleActivity: onDestroy() called
D/LifeCircleActivity: onCreate() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]Activity对象的地址:cn.steve.activitylifecycle.LifeCircleActivity@7e6e82e
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onRestoreInstanceState() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@80a47f5, 2131296581=android.view.AbsSavedState$1@80a47f5, 2131296815=android.view.AbsSavedState$1@80a47f5}}], key=x}]]
D/LifeCircleActivity: onResume() called

小结

配置 orientation 的情况下,和默认情况一致。


配置 configChanges="orientation|screenSize"

根据官方的介绍,这个两个值,在api大于13 之后,应该一起使用

第一次启动

D/LifeCircleActivity: onCreate() called with: savedInstanceState = [null]Activity对象的地址:cn.steve.activitylifecycle.LifeCircleActivity@de950fc
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onResume() called

第一次切换成横屏

D/LifeCircleActivity: onConfigurationChanged() called with: newConfig = [{1.0 ?mcc?mnc zh_CN ldltr sw360dp w640dp h336dp 320dpi nrml long land finger -keyb/v/h -nav/h s.11 themeChanged=0 themeChangedFlags=0}]

再切换成竖屏

D/LifeCircleActivity: onConfigurationChanged() called with: newConfig = [{1.0 ?mcc?mnc zh_CN ldltr sw360dp w360dp h616dp 320dpi nrml long port finger -keyb/v/h -nav/h s.12 themeChanged=0 themeChangedFlags=0}]

小结

当配置了 screenSize 。则不会再销毁重建了,而是回调 onConfigurationChanged 方法。

总结

在不做配置默认的情况下,Activity 是被销毁,然后重新启动的.但是在 manifest 中进行相应的配置之后,就表示 Activity 自行处理配置的更改,将阻止 Activity 的销毁重新启动,而是保持运行状态,并且回调 onConfigurationChanged 方法.官方的建议是万不得已的情况下才能使用.

参考

Activity 在横竖屏切换情况下的生命周期变化的更多相关文章

  1. android activity在横竖屏切换的时候不重新调用onCreate方法

    在安卓系统中,横竖屏切换会默认重新调用onCreate等生命周期方法,如果此时有一些临时数据没有保存下来,很有可能会导致该数据丢失. 因此我们可以进行以下设置,来避免恒切换时重新调用onCreate方 ...

  2. android 横竖屏切换不重走生命周期

    android在系统配置发生改变时,Activity会被重新创建,但是某些情况下我们希望系统配置改变时不会重新创建Activity,这个时候我们可以给Activity指定相对应的configChang ...

  3. Android学习记录(8)—Activity的四种加载模式及有关Activity横竖屏切换的问题

    Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance.以下逐一举例说明他们的区别: standard:Activity ...

  4. Activity嵌套多个Fragment实现横竖屏切换

    一.上图 二.需求 最近项目遇到个横竖屏切换的问题.较为复杂.在此记之. 1.Activity中竖屏嵌套3个Fragment,本文简称竖屏FP1,FP2,FP3. 2.当中竖屏FP1与FP2能够切换为 ...

  5. Android APP 简单高效的禁用横竖屏切换

    默认情况下,Android APP的界面会随着手机方向的改变而改变,当手机处于竖屏状态,APP的界面也处于竖屏状态,而当手机处于横屏状态,APP也会自动切换到横屏状态.一般情况下APP的界面都是为竖屏 ...

  6. Android应用:横竖屏切换总结

    眨眼间,已经到了2016你年春节前,离上一篇博客的时间已经有6个月多,回想起这半年的种种,不得不说,学习和工作实在是太忙了,或许这就是程序员的真实写照吧. 写博客之初,主要的目的还是为了把自己的学习痕 ...

  7. Android横竖屏切换重载问题与小结

    (转自:http://www.cnblogs.com/franksunny/p/3714442.html) (老样子,图片啥的详细文档,可以下载后观看 http://files.cnblogs.com ...

  8. Android 横竖屏切换小结

    (自己体会:每次横竖屏自动切时都会run Activity的onCreate,即相当后重新进入Activity初始化一样:) 转自:http://www.cnblogs.com/franksunny/ ...

  9. Android横竖屏切换小结

    Android横竖屏切换小结 (老样子,图片啥的详细文档,可以下载后观看 http://files.cnblogs.com/franksunny/635350788930000000.pdf) And ...

随机推荐

  1. web性能优化之GZIP压缩

    从服务端优化来说,通过对服务端做压缩配置可以大大减小文本文件的体积,从而使加载文本的速度成倍的加快.目前比较通用的压缩方法是启用gzip压缩.它会把浏览器请求的页面,以及页面中引用的静态资源以压缩包的 ...

  2. iOS 根据url生成二维码贴到底图上

    根据url 生成指定尺寸的二维码图片 UIImage * createBinaryCodeImg(const char * url ,CGFloat size) { //create binary c ...

  3. 为什么我不再用 .NET 框架(网摘)

    觉得好就拿过来收藏了,保留出处链接-凌风 2017年08月23日 14:51:32 hisense_大致若愚 阅读数:9355 .NET平台很棒.真的很棒.直到它不再那么棒.我为什么不再用.NET?简 ...

  4. C++构造函数及成员变量

    class MyClass { public: int m_age; float m_hight; MyClass() { } ~MyClass() { } MyClass(int age, floa ...

  5. IIS配置导入导出

    使用管理员身份运行cmd 应用程序池: # 导出所有应用程序池 %windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\a ...

  6. 日期插件rolldate.js的使用

    日期插件rolldate.js的使用 下载地址:http://www.jq22.com/jquery-info19834 效果: 代码: <!DOCTYPE html> <html ...

  7. laravel5.5源码笔记(五、Pipeline管道模式)

    Pipeline管道模式,也有人叫它装饰模式.应该说管道是装饰模式的一个变种,虽然思想都是一样的,但这个是闭包的版本,实现方式与传统装饰模式也不太一样.在laravel的源码中算是一个比较核心的设计模 ...

  8. IOS 可以连接 蓝牙BLE设备,但是无法发现服务(原创)

    注:转载请标明文章来源,感谢支持作者劳动! 一.问题描述 用iphone手机上的nRF connect软件调试蓝牙通信. 1.nRF52蓝牙demo电路板,烧录一个SDK的程序,iphone手机可以成 ...

  9. Python的print的底层实现

    默认调用 sys.stdout.write() 方法 import sys sys.stdout.write("hello") 会在控制台打印:hello

  10. 每天一个Linux命令之mkdir

    Linux mkdir命令 mkdir [-p] filename 用于创建一个空目录 如果该目录下有相同名称的目录那么会报错 apple@apple-Pro  ~/Documents/java_d ...