Activity Lifecycle—活动生命周期详解

一、前期基础知识储备

上官方文档:Activity Lifecycle

Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

链接:https://developer.android.google.cn/reference/android/app/Activity.html#ActivityLifecycle

了解活动生命周期的意义:掌握活动的生命周期对任何Android开发者来说都非常重要,当你深入了解活动的生命周期之后,就可以写出更加连贯流畅的程序,并在如何合理管理应用资源方面发挥的游刃有余。你的程序将会拥有更好的用户体验。

Android任务栈的了解Android是使用任务栈(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称作返回栈(Back Stack)。栈是一种后进先出的数据结构*后来者居上)。在默认情况下,每当我们启动了一个新的活动,它会在返回栈中入栈,并处于栈顶的位置。而每当我们按下Back键或调用finish()方法去销毁一个活动时,处于栈顶的活动就会出栈,这时前一个入栈的活动就会重新处于栈顶的位置。系统总是会显示处于栈顶的活动给用户

活动生命周期的四种状态

①运行状态,当一个活动位于返回栈的栈顶时,这时活动就处于运行状态。系统最不愿意回收的就是处于运行状态的活动,因为这会带来非常差的用户体验。

②暂停状态,当一个活动不再处于栈顶位置,但仍然可见时,这时活动就进入了暂停状态,比如有一些活动以对话框的形式出现,只会占用屏幕中间的部分位置,处于其下仍然可见的活动就是暂停状态,系统也不愿去回收这种状态的活动(活动对于用户仍然是可见的)。

③停止状态:当一个活动不再处于栈顶位置,并且完全不可见的时候就进入了停止状态,当有其他地方需要内存时,处于停止状态的活动有可能会被系统回收。

④销毁状态:当一个活动从返回栈中移除后就变成销毁状态。系统会最倾向于回收这种状态的活动,从而保证手机内存充足。

二、活动生命周期具体分析:

Activity类中定义了7个回调方法,覆盖了活动生命周期的每隔环节。

onCreate(),它会在活动第一次被创建的时候调用。开发者要在这个方法中完成活动的初始化操作,比如说加载布局、绑定时间等。Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.Always followed by onStart().

onStart(),这个方法在活动由不可见变为可见的时候调用。Called when the activity is becoming visible to the user.
Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

onResume(),这个方法在活动准备好和用户进行交互的时候调用。此时的活动一定位于返回栈的栈顶,并且处于运行状态。Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.Always followed by onPause().

onPause(),这个方法在系统准备去启动或者恢复另一个活动的时候调用。我们通常会在这个方法中将一些消耗CPU的资源释放掉,以及保存一些关键数据,但这个方法的执行速度一定要快,不然会影响到新的栈顶活动的使用。Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.

onStop(),这个方法在活动完全不可见的手调用。它和onPause方法的主要区别在于,如果启动的新活动是一个对话框式的活动,那么onPause()方法会的得到执行,而onStop()方法并不会执行。Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.
Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.

onDestroy(),这个方法在活动被销毁之前调用,之后活动的状态将变成销毁状态。The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called 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 isFinishing() method.

onRestart(),这个方法在活动活动由停止状态变为运行状态时调用。Called after your activity has been stopped, prior to it being started again.Always followed by onStart()

以上7个方法除了onRestart()方法之外,都是两两对应的,从而又将活动分为三个生存期。

完整生存期。活动在onCreate方法和OnDestroy方法之间所经历的,就是完整生存周期。一般情况下,一个活动会在onCreate方法中完成各种初始化操作,而在onDeatroy方法中完成释放内存的操作。

可见生存期。活动在onStart方法和onStop方法之间所经历的,就是可见生存期。在可见生存期内,活动对于用户总是可见的,即便有可能无法和用户进行交互。我们可以通过这两个方法,合理地管理那些对用户的可见的资源。

前台生存期。活动在onResume方法和onPause方法之间所经历的就是前台生存期。在前台生存期内,活动总是处于运行状态,此时的活动是可以和用户进行交互的,我们平时看到和接触最多的也就是处于这个状态下的活动。

小结:到这里我们来个小结,当Activity启动时,依次会调用onCreate(),onStart(),onResume(),而当Activity退居后台时(不可见,点击Home或者被新的Activity完全覆盖),onPause()onStop()会依次被调用。当Activity重新回到前台(从桌面回到原Activity或者被覆盖后又回到原Activity)时,onRestart()onStart()onResume()会依次被调用。当Activity退出销毁时(点击back键),onPause()onStop()onDestroy()会依次被调用,到此Activity的整个生命周期方法回调完成。现在我们再回头看看之前的流程图,应该是相当清晰了吧。嗯,这就是Activity整个典型的生命周期过程。

参考官方给出的图解:


三、活动是四种启动模式(又称加载模式)

活动的启动模式对你来说应该是个全新的概念,在实际项目中我们应该根据特定的需求为每个活动指定恰当的启动模式。启动模式一共有4种,分别是standardsingleTopsingleTasksingleInstance。可以在AndroidManifest.xml中通过给<activity>标签指定android:launchMode属性来选择启动模式。

 

读者想练习Activity相关的知识,可下载某牛客进行Activity生命周期相关题目的练习,提高知识熟练度。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 是一个流行的 JavaScript 框架,它有一套完整的生命周期钩子函数,用于在组件的不同阶段执行特定的操作。下面是 Vue 组件的生命周期钩子函数及其对应的阶段: 1. beforeCreate: 在实例被创建之前调用,此时 data 和 methods 等属性还未初始化。 2. created: 在实例创建完成后调用,此时可以访问 data 和 methods,并可以进行一些初始化操作。 3. beforeMount: 在编译模板之前调用,此时模板还未被渲染成真实的 DOM。 4. mounted: 在编译模板之后调用,此时模板已经被渲染成真实的 DOM,并且可以进行 DOM 操作。 5. beforeUpdate: 在数据更新之前调用,此时可以进行一些更新准备工作。 6. updated: 在数据更新之后调用,此时 DOM 已经被更新。 7. beforeDestroy: 在实例销毁之前调用,此时实例仍然可用。 8. destroyed: 在实例销毁之后调用,此时实例及其所有的监听器都已被移除。 下面是一个简单的示例代码,演示了 Vue 组件的生命周期函数的使用: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Vue Lifecycle Demo</title> </head> <body> <div id="app"> {{ message }} </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> new Vue({ el: '#app', data: { message: 'Hello, Vue!' }, beforeCreate() { console.log('beforeCreate hook') }, created() { console.log('created hook') }, beforeMount() { console.log('beforeMount hook') }, mounted() { console.log('mounted hook') }, beforeUpdate() { console.log('beforeUpdate hook') }, updated() { console.log('updated hook') }, beforeDestroy() { console.log('beforeDestroy hook') }, destroyed() { console.log('destroyed hook') } }) </script> </body> </html> ``` 你可以在浏览器中打开该示例,然后在控制台中查看不同生命周期阶段的输出信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值