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

时间:2022-09-12 19:12:02

【声明】

欢迎转载,但请保留文章原始出处→_→

生命壹号:http://www.cnblogs.com/smyhvae/

文章来源:http://www.cnblogs.com/smyhvae/p/3856555.html

【正文】

一、Activity的生命周期:

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

解释如下:

activity启动的时候:onCreate ---> onStart ---> onResume.
      为了方便说明这里把当前activity称为MainActivity,其他的activity称为SecondActivity、ThirdActivity等等。
      通过上图的我们可以看出activity从running状态跳转到onPause状态的原因是:Another activity comes into the foreground. 也就是说有另外一个actvity被启动并运行了,比如说MainActivity通过startActivity启动了SecondActivity,那么SecondActivity就在ui视图的最顶层了,而MainActivity不再是最顶层的activity了,此时就会onPause了,此时我们依稀还能看到MainActivity,只是MainActivity已经站在他人之后了。

而当MainActivity完全被SecondActivity挡住,完全看不见的时候,此时MainActivity就会onStop了。我们看看上图从onPause到onStop的原因是:the activity is no longer visiable. 也就是说此时MainActivity完全不可见了,从这里我们可以猜测当按下home键的时候,当前activity就会处于onStop的状态。
从actvitiy的生命周期图中,我们可以看出activity是可以停留在onPause和onStop在这两个状态上的,因为可以相应的恢复。

当在MainActivity中按back键,退出时,最终就会走到onDestory,或者在代码中调用finish()的时候也会走到onDestroy这一步。

当MainActivity处于onPause和onStop状态时,当更高优先级的apps需要内存,而此时系统内存不够了,那么android系统就会将MainActivity所在的进程给杀了以释放资源。当然被杀了之后,想重生,那必须得从头来过: onCreate ---> onStart() ---> onResume().

二、成对儿的生命周期函数:(相同的颜色表示一对)

绘制表格如下:

生命周期函数

调用时机

举例

onCreate

在Activity对象被第一次创建时调用

买车

onStart

当Activity变得可见时调用

打火,启动

onResume

当Activity开始准备和用户交互时调用

踩油门,驱动汽车前进

onPause

当系统即将启动另外一个Activity之前调用

松开油门

onStop

当前Activity变得不可见时调用

熄火

onDestroy

当前Activity被销毁之前调用

车辆报废

onRestart

当一个Activity再次启动之前调用

 

一个Activity的对象的生命周期可类比于人的生命周期。一个阶段将调用对应的生命周期函数,即每个阶段需要做每个阶段应该做的事。

注意:生命周期函数是由系统调用的。开发人员只需要复写函数中的内容即可。

三、Activity对象的状态:

Resumed:Activity对象出于运行状态。

Paused:另一个Activity位于前端,但是本Activity还可见。

Paused状态常用于:另一个Activity为对话框,弹出来之后只占据了屏幕的一小部分;但后面的Activity还是可见的,但是处于paused状态。

注:一个Activity出于paused状态时,系统并不会释放资源。释放资源你的操作要靠开发者来完成。

Stopped:另一个Activity位于前端,完全遮挡本Activity。

官方文档解释如下:

Managing the Activity Lifecycle

Managing the lifecycle of your activities by implementing callback methods is crucial to developing a strong and flexible application. The lifecycle of an activity is directly affected by its association with other activities, its task and back stack.

An activity can exist in essentially three states:

Resumed
The activity is in the foreground of the screen and has user focus. (This state is also sometimes referred to as "running".)
Paused
Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
Stopped
The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but isnot attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.

If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.

四、举一个实例:

现在有两个Activity,当用户点击当前默认的MainActivity中的按钮后,会跳到SecondActivity。生命周期如下:

启动默认Activity时:

MainActivity:onCreate

MainActivity:onStart

MainActivity: onResume

点击按钮后,跳到SecondActivity:(MainActivity被保留至栈底)

MainActivity:onPause

SecondActivity:onCreate

SecondActivity:onStart

SecondActivity: onResume

MainActivity: onStop

按返回菜单后,回到MainActivity:(SecondActivity被销毁)

SecondActivity: onPause

MainActivity:onRestart

MainActivity:onStart

MainActivity: onResume

SecondActivity: onStop

SecondActivity: onDestroy

参考链接:http://www.cnblogs.com/hibraincol/archive/2012/03/06/2382120.html

我的公众号

想学习代码之外的软技能?不妨关注我的微信公众号:生命团队(id:vitateam)。

扫一扫,你将发现另一个全新的世界,而这将是一场美丽的意外:

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