Android 组件系列 -- Activity的生命周期

本篇记录的是对于 Activity 普通生命周期的总结,或者说是 standand 启动模式下 Activity 的生命周期。 文章先是对生命周期的方法抽象描述了一番,简单说就是概念性的描述;在概念之后,还会贴上很多代码,用于对生命周期理解的补充。

抽象概述

先放一张特别经典的Activity生命周期图:
详细讲解生命周期:
onCreate() :
Called when the activity is starting.
This is where most initialization should go: calling {@link #setContentView(int)} to inflate the activity’s UI…..
Activity 启动第一个运行的方法,创建Activity。
你可以在其中做一些必要的初始化工作,比如 setContentView(): 给Activity 设置 UI 资源。
此时的Acivity 依然处于不可见的状态。
onStart() :
Called after {@link #onCreate} — or after {@link #onRestart} when the activity had been stopped, but is now again being displayed to the user. It will be followed by {@link #onResume}.
Activity 显示在用户眼前的时候被调用。
但是调用的顺序,却并不是一定在第二个被调用:
情况一:当 Activity 首次启动的时候,onStart() 紧跟在 onCreate() 之后被调用。
情况二:当一个已经停止(stopped)的Activity,再次显示在用户眼前时,先是调用了 onRestart(),而 onStart() 紧跟其后。
此时的Acivity 虽然处于可见状态,但是却无法获取焦点与用户交互。
onResume() :
Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or {@link #onPause}, for your activity to start interacting with the user.
This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.
当 Activity 一切准备就绪,UI 也准备就绪,Activity 可以获取焦点,与用户进行交互的时候被调用。
调用的顺序,在前面 onStart() 的英文描述中已经提到,onStart() 之后紧跟着就是调用 onResume()。
此时的Acivity 既处于可见状态,也可获取焦点与用户交互。
onPause() :
Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed.
The counterpart to {@link #onResume}.
When activity B is launched in front of activity A, this callback will be invoked on A.
B will not be created until A’s {@link #onPause} returns, so be sure to not do anything lengthy here.
当 Activity 准备进入后台,或者被另外一个 Activity 覆盖时,第一时间被调用。
这个方法对应着 onResume() 方法。
此时的Acivity 处于可见状态,但是无法获取焦点与用户交互。
onStop() :
Called when you are no longer visible to the user.
You will next receive either {@link #onRestart}, {@link #onDestroy}, or nothing, depending on later user activity.
此时Activity对于用户已经不可见了,而接下来可能会调用到 onRestart,onDestroy,或者什么都不调用,这主要看用户的操作了。
此时的Acivity 已经处于不可见状态了。
onDestroy() :
Perform any final cleanup before an activity is destroyed.
This can happen either because the activity is finishing (someone called {@link #finish} on it, or because the system is temporarily destroying this instance of the activity to save space.
You can distinguish between these two scenarios with the {@link #isFinishing} method.
这个方法是在 Activity 销毁之前调用,用于做一些清理工作,也会因为 finish() 被调用而调用。
此时的Acivity 已经被销毁了。
onRestart() :
Called after {@link #onStop} when the current activity is being re-displayed to the user (the user has navigated back to it).
It will be followed by {@link #onStart} and then {@link #onResume}.
onRestart() 这个方法是调用过 onStop() 的Activity,但是没有销毁,当再次显示的时候会被调用,之后就会紧接着调用 onStart() 和 onResume()。
此时的Acivity 介于 onStart() 之前。

代码理解

场景一:这是普通的 MainActivity 被 普通的 SecondActivity 覆盖的情形

具体流程: MainActivity 跳转到 SecondActivity; SecondActivity 返回 MainActivity

  

生命周期流程图:左图为 Main 跳转到 Second; 右图为 Secon 返回 Main

 

总结场景一:最普通的场景往往调用最全的生命周期方法

Main 创建过程,依次执行:
Main.onCreate() — Main.onStart() — Main.onResume()

跳转直到Second显示,依次执行:
Main.onPause() — Second.onCreate() — Second.onStart() — Second.onResume() — Main.onStop()

从Second 返回 Main,依次执行:
Second.onPause()—Main.onRestart()—Main.onStart()—Main.onResume()—Second.onStop()—Second.onDestroy()



场景二:如果在场景一的基础上将 SecondActivity 变为一个透明的 Activity,然后再返回 Main 呢?

具体流程: MainActivity 跳转到 SecondActivity; 由SecondActivity 跳转回 MainActivity。

      

总结场景二:在实际开发中,如果用到了这种情形,一般透明的 Activity 作为一个数据或者交互的中间人。

Main 创建过程,依次执行:
Main.onCreate() — Main.onStart() — Main.onResume()

跳转直到Second显示,依次执行:
Main.onPause() — Second.onCreate() — Second.onStart() — Second.onResume()
(与场景一不同的是,由于Main一直处于可见状态,就算是被透明的Second覆盖了,也仅仅是失去了获取焦点进行交互的能力,所以不会调用 onStop() 方法)

从Second 返回 Main,依次执行:
Second.onPause() — Main.onResume() — Second.onStop() — Second.onDestroy()
(由于 Main 仅仅是失去了焦点,依然可见,而且没有调用过 onStop() 方法,那么从 API 的描述中,只有调用过 onStop() 的方法,才有可能调用 onRestart(),而onStart() 自然也不会被调用。)



场景三:如果在场景二的基础上不返回 Main,而是由 Second 跳转到一个正常的 ThirdActivity ,再依次返回,又会有哪些生命周期被调用呢?

具体流程: MainActivity 跳转到 SecondActivity; SecondActivity 跳转 ThirdActivity,然后依次返回,其中 SecondActivty 为一个透明的 Activity。

      


总结场景三:

Main 创建过程,依次执行:
Main.onCreate() — Main.onStart() — Main.onResume()

跳转直到Second显示,依次执行:
Main.onPause() — Second.onCreate() — Second.onStart() — Second.onResume()

从Second 跳转 Third,依次执行:
Second.onPause() — Third.onCreate() — Third.onStart() — Third.onResume() — Main.onStop() — Second.onStop()
(注意,这里由于 Third 不是一个透明的 Activity,在Thrid覆盖之后,Main不可见了,那么自然会调用到 Main.onStop() 方法)

由 Third 返回 Second,依次执行:
Third.onPause() — Main.onRestart() — Main.onStart() — Second.onRestart() — Second.onStart() — Second.onResume() — Third.onStop() — Third.onDestroy()
(在上一步 Main 调用过 onStop 方法了,那么在 Thrid.onPause() 之后,会依次调用 Main.onRestart(),Main.onStart() 方法,但是Main此时依然被Second遮住,依然处于可见不可交互的状态,不会调用 Main.onResume(),Second 自然不用说,正常走完生命周期。)

由 Second 返回 Main,依次执行:
Second.onPause() — Main.onResume() — Second.onStop() — Second.onDestroy()



至此,Activity的生命周期有了一个初步的理解,更深入的就是对于 Activity 状态的保存,启动模式这些情况下,生命周期调用的理解了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值