简述Activity⽣命周期
copy from :
基于Android 6.0的源码剖析,分析android Activity启动流程中ActivityManagerService所扮演的⾓⾊
⼀、概述
上⼀篇⽂章,介绍了startActivity是如何⼀步步创建的,再来看看⽣命周期的控制。先来⼀张官⽅的Activity状态转换图:
Activity的⽣命周期中只有在以下3种状态之⼀,才能较长时间内保持状态不变。
Resumed(运⾏状态):Activity处于前台,且⽤户可以与其交互。
Paused(暂停状态): Activity被在前台中处于半透明状态或者未覆盖全屏的其他Activity部分遮挡。暂停的Activity不会接收⽤户输⼊,也⽆法执⾏任何代码。
Stopped(停⽌状态):Activity被完全隐藏,且对⽤户不可见;被视为后台Activity。停⽌的Activity实例及其诸如成员变量等所有状态信息将保留,但它⽆法执⾏任何代码。
除此之外,其他状态都是过渡状态(或称为暂时状态),⽐如onCreate(),onStart()后很快就会调⽤onResume()⽅法。
⼆. ⽣命周期
2.1 进程间通信
对于App来说,其Activity的⽣命周期执⾏是与系统进程中的ActivityManagerService有⼀定关系的,接下来从进程和线程的⾓度来分析Activity的⽣命周期,这⾥涉及到系统进程和应⽤进程:
system_server进程是系统进程,Java framework框架的核⼼载体,⾥⾯运⾏了⼤量的系统服务,⽐如这⾥提供ApplicationThreadProxy(简称ATP),ActivityManagerService(简称AMS),这个两个服务都运⾏在system_server进程的不同线程中,由于ATP和AMS都是基于IBinder接⼝,都是binder线程,binder线程的创建与销毁都是由binder驱动来决定的。
App进程是应⽤程序所在进程,主线程主要负责Activity/Service等组件的⽣命周期以及UI相关操作都运⾏在这个线程;另外,每个App进程中⾄少会有两个binder线程 ApplicationThread(简称AT)和ActivityManagerProxy(简称AMP),除了下图中所⽰的线程,其实还有很多线程,⽐如signal catcher线程等。
Binder⽤于不同进程之间通信,由⼀个进程的Binder客户端向另⼀个进程的服务端发送事件,⽐如图中线程2向线程4发送事务;⽽handler⽤于同⼀个进程中不同线程的通信,⽐如图中线程4向主线程发送消息。
结合图说说Activity⽣命周期,⽐如暂停Activity的流程如下:
线程1的AMS中调⽤线程2的ATP来发送事件;(由于同⼀个进程的线程间资源共享,可以相互直接调⽤,但需要注意多线程并发问题)线程2通过binder将暂停Activity的事件传输到App进程的线程4;
线程4通过handler消息机制,将暂停Activity的消息发送给主线程;
主线程在looper.loop()中循环遍历消息,当收到暂停Activity的消息(PAUSE_ACTIVITY)时,便将消息分发给
ActivityThread.H.handleMessage()⽅法,再经过⽅法的层层调⽤,最后便会调⽤到Pause()⽅法。
这便是由AMS完成了onPause()控制,那么同理Activity的其他⽣命周期也是这么个流程来进⾏控制的。
2.2 App主线程
每个App都有⼀个主线程,⼤家常说主线程是ActivityThread,其实这个说法是⽋妥当的,⾸先何为线程?⼀般来说Java层创建线程往往是继承Thread对象或者实现Runnable,再看看ActivityThread,会发现该对象并没有继承任何对象。准确说法ActivityThread是运⾏在主线程的对象,充当着主线程的职责。
那主线程到底是哪个呢,这个问题涉及到Linux进程与线程的理解,本质上来说⼤家常说的主线程就是app⾸次启动时创建的进程,对于Linux来说进程与线程都是⼀个task_struct结构体,除了是否有独⽴资源,并没有什么区别。
那有为何说充当着主线程的职责呢?这是由于进程在创建之初会为主线程创建Looper对象,这个便是⽤来维护Activity的⽣命周期。关于更多详细,可查看我之前在知乎的⼀个回答
2.3 枢纽中⼼
Activity的⽣命周期,都是其他线程通过handler发送消息给主线程,那么主线程中的ActivityThread的内部类H控制整个核⼼消息处理机制,通过H.handleMessage()来控制Activity的⽣命周期,在H类中共定义了50种消息。
private class H extends Handler {
public static final int LAUNCH_ACTIVITY = 100;
public static final int PAUSE_ACTIVITY = 101;
public static final int PAUSE_ACTIVITY_FINISHING= 102;
public static final int STOP_ACTIVITY_SHOW = 103;
public static final int STOP_ACTIVITY_HIDE = 104;
public static final int SHOW_WINDOW = 105;
public static final int HIDE_WINDOW = 106;
public static final int RESUME_ACTIVITY = 107;
public static final int SEND_RESULT = 108;
public static final int DESTROY_ACTIVITY = 109;
public static final int BIND_APPLICATION = 110;
public static final int EXIT_APPLICATION = 111;
public static final int NEW_INTENT = 112;
public static final int RECEIVER = 113;
public static final int CREATE_SERVICE = 114;
public static final int SERVICE_ARGS = 115;
public static final int STOP_SERVICE = 116;
public static final int CONFIGURATION_CHANGED = 118;
public static final int CLEAN_UP_CONTEXT = 119;
public static final int GC_WHEN_IDLE = 120;
public static final int BIND_SERVICE = 121;
public static final int UNBIND_SERVICE = 122;
public static final int DUMP_SERVICE = 123;
public static final int LOW_MEMORY = 124;
public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
public static final int RELAUNCH_ACTIVITY = 126;
public static final int PROFILER_CONTROL = 127;
public static final int CREATE_BACKUP_AGENT = 128;
public static final int DESTROY_BACKUP_AGENT = 129;
public static final int SUICIDE = 130;
public static final int REMOVE_PROVIDER = 131;
public static final int ENABLE_JIT = 132;
public static final int DISPATCH_PACKAGE_BROADCAST = 133;
public static final int SCHEDULE_CRASH = 134;
public static final int DUMP_HEAP = 135;
public static final int DUMP_ACTIVITY = 136;
public static final int SLEEPING = 137;
public static final int SET_CORE_SETTINGS = 138;
public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
public static final int TRIM_MEMORY = 140;
public static final int DUMP_PROVIDER = 141;
public static final int UNSTABLE_PROVIDER_DIED = 142;
public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
public static final int INSTALL_PROVIDER = 145;
public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
public static final int CANCEL_VISIBLE_BEHIND = 147;
public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
public static final int ENTER_ANIMATION_COMPLETE = 149;
}
主线程每到收到其他线程发送过来的不同的Handler消息,则都会触发相应的H.handleMessage,下⾯列举跟Activity相关的⼀些常见消息。
LAUNCH_ACTIVITY
RELAUNCH_ACTIVITY
RESUME_ACTIVITY
NEW_INTENT
PAUSE_ACTIVITY / PAUSE_ACTIVITY_FINISHING
STOP_ACTIVITY_SHOW / STOP_ACTIVITY_HIDE
DESTROY_ACTIVITY
⼀般来说收到消息,都会调⽤相应handlexxx⽅法。⽐如,LAUNCH_ACTIVITY则对应handleLaunchActivity, RESUME_ACTIVITY则对
应handleResumeActivity等。
public void handleMessage(Message msg) {
switch (msg.what) {
case LAUNCH_ACTIVITY: {
final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo, rpatInfo);
handleLaunchActivity(r, null);
} break;
case RELAUNCH_ACTIVITY: {
ActivityClientRecord r = (ActivityClientRecord)msg.obj;
handleRelaunchActivity(r);
} break;
case PAUSE_ACTIVITY:
handlePauseActivity((IBinder)msg.obj, false, (msg.arg1&1) != 0, msg.arg2,
(msg.arg1&2) != 0);
maybeSnapshot();
break;
case STOP_ACTIVITY_SHOW:
handleStopActivity((IBinder)msg.obj, true, msg.arg2);
break;
case STOP_ACTIVITY_HIDE:
handleStopActivity((IBinder)msg.obj, false, msg.arg2);
break;
case RESUME_ACTIVITY:
handleResumeActivity((IBinder) msg.obj, true, msg.arg1 != 0, true);
break;
case DESTROY_ACTIVITY:
handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0, msg.arg2, false);
break;
...
}
}
先简单列举先调⽤链可能涉及的⽅法(注:并⾮每次都能同时进⼊如下调⽤链的每个分⽀,先⼤致列举,后续再展开)
三. 调⽤链
3.1 启动应⽤
消息:LAUNCH_ACTIVITY
调⽤链
ActivityThread.handleLaunchActivity
ActivityThread.handleConfigurationChanged
ActivityThread.performConfigurationChanged
ActivityThread.performLaunchActivity
LoadedApk.makeApplication
Instrumentation.callApplicationOnCreate
Instrumentation.callActivityOnCreate
Activity.performCreate
Instrumentation.callActivityonRestoreInstanceState
Activity.performRestoreInstanceState
ActivityThread.handleResumeActivity
ActivityThread.performResumeActivity
Activity.performResume
Activity.performRestart
Instrumentation.callActivityOnRestart
Activity.performStart
Instrumentation.callActivityOnStart
Instrumentation.callActivityOnResume
采⽤缩进⽅式,来代表⽅法的调⽤链,相同缩进层的⽅法代表来⾃位于同⼀个调⽤⽅法⾥。callActivityOnCreate和callActivityonRestoreInstanceState相同层级,代表都是由上⼀层级的ActivityThread.performLaunchActivity()⽅法中调⽤。
App⾓度
调⽤链过程层层调⽤,但对上层应⽤是透明的,App开发者只需要覆写其中重要的回调函数即可,故此处所说的App⾓度,便是指App开发者来说可见之处。经过上述的调⽤链,依次会执⾏下⾯回调⽅法。
1. ComponentCallbacks
2. Create()
3. Create()
简述android概述4. RestoreInstanceState()
5. Restart()
6. Start()
7. Resume()
Application和Activity都实现了ComponentCallbacks2接⼝;所以Application和Activity会先执⾏onConfi
gurationChanged()回调⽅法。在前⾯说过onCreate()是过渡状态,紧跟着会执⾏handleResumeActivity()⽅法,然后就进⼊Resumed状态。
3.2 恢复应⽤
消息:RESUME_ACTIVITY
调⽤链
ActivityThread.handleResumeActivity
ActivityThread.performResumeActivity
Activity.performResume
Activity.performRestart
Instrumentation.callActivityOnRestart
Activity.performStart
Instrumentation.callActivityOnStart
Instrumentation.callActivityOnResume
App⾓度
1. Restart()
2. Start()
3. Resume()
App处于运⾏状态,UI可见。
3.3 暂停应⽤
msg: PAUSE_ACTIVITY
调⽤链
ActivityThread.handlePauseActivity
ActivityThread.performPauseActivity
ActivityThread.callCallActivityOnSaveInstanceState
Instrumentation.callActivityOnSaveInstanceState
Activity.performSaveInstanceState
Instrumentation.callActivityOnPause
Activity.performPause
App⾓度
1. SaveInstanceState()
2. Pause()
根据saveState是否true决定是否执⾏callCallActivityOnSaveInstanceState()分⽀,从⽽决定是否回调onRestoreInstanceState()⽅法
3.4 停⽌应⽤
msg: STOP_ACTIVITY_HIDE
调⽤链
ActivityThread.handleStopActivity
ActivityThread.performStopActivityInner
ActivityThread.callCallActivityOnSaveInstanceState
Instrumentation.callActivityOnSaveInstanceState
Activity.performSaveInstanceState
ActivityThread.performStop
Activity.performStop
Instrumentation.callActivityOnStop
updateVisibility
H.post(StopInfo)
AMP.activityStopped
AMS.activityStopped
ActivityStack.activityStoppedLocked
ProcessRecord.kill
ApplicationThread.scheduleExit
AMS.cleanUpApplicationRecordLocked
AMS.updateOomAdjLocked
App⾓度
1. SaveInstanceState
2. Stop
在停⽌Activity的过程,会有⼀个trimApplications()的操作,主要是kill空进程,将当前进程退出loop循环,清理应⽤的上下⽂环境,并且更新进程的Adj值。
3.5 销毁应⽤
msg: DESTROY_ACTIVITY
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论