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



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 方法.官方的建议是万不得已的情况下才能使用.

参考

原文地址:https://www.cnblogs.com/jnienv/p/10575533.html

时间: 2024-10-07 21:05:59

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

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

android在系统配置发生改变时,Activity会被重新创建,但是某些情况下我们希望系统配置改变时不会重新创建Activity,这个时候我们可以给Activity指定相对应的configChanges属性,在manifest加入 android:configChanges="keyboardHidden|orientation|screenSize" orientation:屏幕方向发生了改变,如旋转屏幕 screenSize:屏幕尺寸发生了变化,如旋转屏幕 keyboardHid

android开发之activity横竖屏切换时的生命周期以及横竖屏切换时的资源适配方案

背景:之前有过两篇写activity的博客 android之activity的生命周期详解:详细介绍了activity的整个生命周期.各状态间的转换和返回桌面时保存activity的状态 android之activity中onSaveInstanceState和onRestoreInstanceState的触发时机:介绍了activity中这两个方法的触发时机和作用 本篇博客会牵扯到里面的内容,如果你都有所了解可以直接往下看,如果不了解可以进去回忆下. 问题:在做应用的退出对话框时,发现如果对话

Android之Activity生命周期及横竖屏切换

Activity有三个状态: 当它在屏幕前台时(位于当前任务堆栈的顶部),它是激活或运行状态.它就是响应用户操作的Activity. 当它失去焦点但仍然对用户可见时(如右图),它处于暂停状态.即在它之上有另外一个Activity.这个Activity也许是透明的,或者没有完全覆盖全屏,所以被暂停的Activity仍对用户可见.暂停的Activity仍然是存活状态(它保留着所有的状态和成员信息并保持和窗口管理器的连接),但系统处于极低内存时仍然可以杀死这个Activity. 完全被另一个Activ

Activity横竖屏切换

默认情况下,当"屏幕方向"或"键盘显示隐藏" 变化时都会销毁当前Activity,创建新的Activity. 如果不希望重新创建Activity实例,可以按如下配置Activity: <activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation"> 注意Android3.2横竖屏幕切换时以上配置con

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

一.上图 二.需求 近期项目遇到个横竖屏切换的问题,较为复杂,在此记之. 1.Activity中竖屏嵌套3个Fragment,本文简称竖屏FP1,FP2,FP3. 2.其中竖屏FP1与FP2可以切换为横屏的FL1,FL2,即竖屏FP1切换到对应的横屏FL1,竖屏FP2对应切换到横屏FL2. 3.FP3不允许横竖屏切换. 4.竖屏FP1,FP2,FP3用ViewPager实现左右滑动切换. 5.横屏的FL1,FL2用布局中的切换按扭实现左右切换,不允许滑动切换. 看到这需求有点儿晕菜了吧!!!呵呵

Activity横竖屏切换的问题

Activity在横竖屏切换的时候会重新走生命周期的方法,这样做的话会导致一些问题 比如我们在界面上录入的一些数据,但因为重新走了生命周期的方法onCreate()方法,这样就会导致前功尽弃,所以就想办法,在横竖屏切换的时候不能让其重新OnCreate(),Android中我们可以在清单文件中对应的Activity使用如下的属性  android:configChanges="keyboardHidden|orientation|screenSize"  这样就可以避免此类事情的发生.

Android应用:横竖屏切换总结

眨眼间,已经到了2016你年春节前,离上一篇博客的时间已经有6个月多,回想起这半年的种种,不得不说,学习和工作实在是太忙了,或许这就是程序员的真实写照吧. 写博客之初,主要的目的还是为了把自己的学习痕迹记录下来,写的东西比较基础,也不多,算是一种督促,希望能坚持地学习下去,因为学识不存在暴发户,靠的是积累.如果对自己过去半年的学习给个评价,我还是比较满意的,前期定下来的目标都基本都达到了.单凭这个,我就觉得今年的新年会是个好年. 说完过去,那么接着就是将来.因为现在的工作环境上外网不大方便,而且

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

(转自:http://www.cnblogs.com/franksunny/p/3714442.html) (老样子,图片啥的详细文档,可以下载后观看 http://files.cnblogs.com/franksunny/635350788930000000.pdf) Android手机或平板都会存在横竖屏切换的功能,通常是由物理重力感应触发的,但是有时候也不尽然,通常在设置里面我们可以对手机的横竖屏切换进行关闭,操作界面如下 只需要点击下“屏幕旋转”按钮就可以关闭横竖屏切换了. 一.禁止AP

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

默认情况下,Android APP的界面会随着手机方向的改变而改变,当手机处于竖屏状态,APP的界面也处于竖屏状态,而当手机处于横屏状态,APP也会自动切换到横屏状态.一般情况下APP的界面都是为竖屏设计的,一旦自动切换到横屏,界面可能就无法直视了.而且每次屏幕方向切换,当前的页面都会销毁并重新创建. 下面先做一个简单的演示 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml