AMS之Activity生命周期原理

一:开始请求执行启动Activity

MyActivity.startActivity() Activity.startActivity() Activity.startActivityForResult Instrumentation.execStartActivty ActivityManagerNative.getDefault().startActivityAsUser()

二:ActivityManagerService接收启动Activity的请求
ActivityManagerService.startActivity() ActivityManagerService.startActivityAsUser() ActivityStackSupervisor.startActivityMayWait() ActivityStackSupervisor.startActivityLocked() ActivityStackSupervisor.startActivityUncheckedLocked() ActivityStackSupervisor.startActivityLocked() ActivityStackSupervisor.resumeTopActivitiesLocked() ActivityStackSupervisor.resumeTopActivityInnerLocked()

三:执行栈顶Activity的onPause方法
ActivityStack.startPausingLocked() IApplicationThread.schudulePauseActivity() ActivityThread.sendMessage() ActivityThread.H.sendMessage(); ActivityThread.H.handleMessage() ActivityThread.handlePauseActivity() ActivityThread.performPauseActivity() Activity.performPause() Activity.onPause() ActivityManagerNative.getDefault().activityPaused(token) ActivityManagerService.activityPaused() ActivityStack.activityPausedLocked() ActivityStack.completePauseLocked() ActivityStack.resumeTopActivitiesLocked() ActivityStack.resumeTopActivityLocked() ActivityStack.resumeTopActivityInnerLocked() ActivityStack.startSpecificActivityLocked

四:启动Activity所属的应用进程
ActivityManagerService.startProcessLocked() Process.start() ActivityThread.main() ActivityThread.attach() ActivityManagerNative.getDefault().attachApplication() ActivityManagerService.attachApplication()

五:执行启动Acitivity
ActivityStackSupervisor.attachApplicationLocked() ActivityStackSupervisor.realStartActivityLocked() IApplicationThread.scheduleLauncherActivity() ActivityThread.sendMessage() ActivityThread.H.sendMessage() ActivityThread.H.handleMessage() ActivityThread.handleLauncherActivity() ActivityThread.performLauncherActivity() Instrumentation.callActivityOnCreate() Activity.onCreate() ActivityThread.handleResumeActivity() ActivityThread.performResumeActivity() Activity.performResume() Instrumentation.callActivityOnResume() Activity.onResume() ActivityManagerNative.getDefault().activityResumed(token)

六:栈顶Activity执行onStop方法
Looper.myQueue().addIdleHandler(new Idler()) Idler.queueIdle() ActivityManagerNative.getDefault().activityIdle() ActivityManagerService.activityIdle() ActivityStackSupervisor.activityIdleInternalLocked() ActivityStack.stopActivityLocked() IApplicationThread.scheduleStopActivity() ActivityThread.scheduleStopActivity() ActivityThread.sendMessage() ActivityThread.H.sendMessage() ActivityThread.H.handleMessage() ActivityThread.handleStopActivity() ActivityThread.performStopActivityInner() ActivityThread.callCallActivityOnSaveInstanceState() Instrumentation.callActivityOnSaveInstanceState() Activity.performSaveInstanceState() Activity.onSaveInstanceState() Activity.performStop() Instrumentation.callActivityOnStop() Activity.onStop()

总结: 1、Activity的启动流程一般是通过调用startActivity或者是startActivityForResult来开始的
2、startActivity内部也是通过调用startActivityForResult来启动Activity,只不过传递的requestCode小于0
3、Activity的启动流程涉及到多个进程之间的通讯这里主要是ActivityThread与ActivityManagerService之间的通讯
4、ActivityThread向ActivityManagerService传递进程间消息通过ActivityManagerNative,ActivityManagerService向ActivityThread进程间传递消息通过IApplicationThread。
【AMS之Activity生命周期原理】5、ActivityManagerService接收到应用进程创建Activity的请求之后会执行初始化操作,解析启动模式,保存请求信息等一系列操作。
6、ActivityManagerService保存完请求信息之后会将当前系统栈顶的Activity执行onPause操作,并且IApplication进程间通讯告诉应用程序继承执行当前栈顶的Activity的onPause方法;
7、ActivityThread接收到SystemServer的消息之后会统一交个自身定义的Handler对象处理分发;
8、ActivityThread执行完栈顶的Activity的onPause方法之后会通过ActivityManagerNative执行进程间通讯告诉ActivityManagerService,栈顶Actiity已经执行完成onPause方法,继续执行后续操作;
9、ActivityManagerService会继续执行启动Activity的逻辑,这时候会判断需要启动的Activity所属的应用进程是否已经启动,若没有启动则首先会启动这个Activity的应用程序进程;
10、ActivityManagerService会通过socket与Zygote继承通讯,并告知Zygote进程fork出一个新的应用程序进程,然后执行ActivityThread的mani方法;
11、在ActivityThead.main方法中执行初始化操作,初始化主线程异步消息,然后通知ActivityManagerService执行进程初始化操作;
12、ActivityManagerService会在执行初始化操作的同时检测当前进程是否有需要创建的Activity对象,若有的话,则执行创建操作;
13、ActivityManagerService将执行创建Activity的通知告知ActivityThread,然后通过反射机制创建出Activity对象,并执行Activity的onCreate方法,onStart方法,onResume方法;
14、ActivityThread执行完成onResume方法之后告知ActivityManagerService onResume执行完成,开始执行栈顶Activity的onStop方法;
15、ActivityManagerService开始执行栈顶的onStop方法并告知ActivityThread;
16、ActivityThread执行真正的onStop方法;

    推荐阅读