太多了,笔者不想写,

读者可通过PlantUML插件查看如下PUML文件生成的时序图。

补充说明下,Android31版本。

@startuml'https://plantuml.com/sequence-diagramskinparam dpi 800scale 15000 widthscale 5000 heightautonumber==Launcher==Launcher -> Activity:startActivity(intent)Activity->Activity:startActivity(intent,bundle)Activity->Activity:startActivityForResultActivity->Instrumentation:execStartActivitynote leftactivity通过调用内部成员mInstrumentation与atms交互,即将开始进入系统进程end noteInstrumentation->ActivityTaskManagerService.Stub:startActivity==SystemService==ActivityTaskManagerService.Stub->ActivityTaskManagerService:StartActivityActivityTaskManagerService->ActivityTaskManagerService:startActivityAsUserActivityTaskManagerService->ActivityTaskManagerService:startActivityAsUserActivityTaskManagerService->ActivityStarter:executeActivityStarter->ActivityStarter:executeRequestnote left这里,会验证启动参数的合法性,随后,创建一个ActivityRecord作为一个Activity的唯一存在标识end noteActivityStarter->ActivityStarter:startActivityUncheckedActivityStarter->ActivityStarter:startActivityInnernote left此函数的功能,如果没有Stack,就要新创建一个Stack,也就是ActivityStack,保存到mTargetTask中,通过setNewTask将待启动的ActivityRecord和新创建的Stack绑定,并且将其置于Stack Top,see ActivityStack::startActivityLocked()see ActivityStack::moveToFront()end noteActivityStarter->ActivityStack:startActivityLockednote left这就是绑定ActivityRecord和新创建的Stack(Task)end noteActivityStack->RootWindowContainer:resumeFocusedStacksTopActivitiesnote left对于新启动的Activity,走此分支end noteRootWindowContainer->ActivityStack:resumeTopActivityUncheckedLockednote left此ActivityStack是13步新创建的,开始调度Activityend noteActivityStack->ActivityStack:resumeTopActivityInnerLockednote left这一步很关键,因为要Resume即将启动的Activity,那么,首先要检查即将启动的Activity在没在栈顶,如果不在,就要去pause栈顶的Activityend noteActivityStack->TaskDisplayArea:pauseBackStacksnote left这里返回是否在pause栈顶的Activity,一般这里会返回true,因为需要pause Launcher,传入next参数即topRunningActivityend notenote right逻辑是去topStack中去判断是否有可见Activity,如果有则pause掉,否则返回false,end noteTaskDisplayArea->ActivityStack:startPausingLockednote right开始pause Launcher,通过mAtmService(ATMS)获取生命周期管理,执行生命周期PauseActivityItem事务end noteActivityStack->ClientLifecycleManager:scheduleTransactionnote left注意,这里传的参数有PauseActivityItemend noteClientLifecycleManager->ClientLifecycleManager:scheduleTransactionClientLifecycleManager->ClientTransaction:scheduleClientTransaction->IApplicationThread:scheduleTransactionnote right进入Launcher进程,去执行Pause事务end noteTaskDisplayArea-->ActivityStack:pauseBackStacks:trueActivityStack-->RootWindowContainer:resumeTopActivityInnerLocked:trueRootWindowContainer-->ActivityStarter:resumeTopActivityUncheckedLocked:trueActivityStarter-->ActivityStarter:ActivityStarter:START_SUCCESS==Launcher进程==IApplicationThread->ActivityThread:scheduleTransactionActivityThread->ActivityThread:sendMessage(159,transaction)ActivityThread->Handler:sendMessagenote right这里159代表事务EXECUTE_TRANSACTION一顿Looper后可以直接看handleMessage处end noteHandler->Handler:handleMessageActivityThread->TransactionExecutor:executeTransactionExecutor->TransactionExecutor:executeLifecycleStateTransactionExecutor->PauseActivityItem:executePauseActivityItem->ActivityThread:handlePauseActivitynote right熟悉的ActivityThread,开始pause Activityend noteActivityThread->ActivityThread:performPauseActivityActivityThread->ActivityThread:performPauseActivityIfNeededActivityThread->Instrumentation:callActivityOnPauseInstrumentation->Activity:performPauseActivity->Activity:onPauseTransactionExecutor->PauseActivityItem:postExecutePauseActivityItem->ActivityTaskManagerService.Stub:activityPausednote left通知系统进程,Launcher已经pauseend note==SystemService==ActivityTaskManagerService->ActivityRecord:activityPaused(false/*timeout*/)note right拓展:这里就是其中一种ANR,如果没超时就移除anr回调,end noteActivityRecord->ActivityStack:completePauseLocked(true/*resumeNext*/,null)ActivityStack->RootWindowContainer:resumeFocusedStacksTopActivitiesRootWindowContainer->ActivityStack:resumeTopActivityUncheckedLockednote right找到下一个即将启动的ActivityStack,开始Resumeend noteActivityStack->ActivityStack:resumeTopActivityInnerLockedActivityStack->TaskDisplayArea:pauseBackStacksTaskDisplayArea-->ActivityStack:pauseBackStacks:falseActivityStack->ActivityStackSupervisor:startSpecificActivitynote right这里通过ActivityRecord去获取WindowProcessController,这个和进程相关,第一次启动肯定没有对应进程,那么就需要创建进程end noteActivityStackSupervisor->ActivityTaskManagerService:startProcessAsyncActivityTaskManagerService->ActivityManagerInternal:startProcessnote right这里通过handler调用到此处,ActivityManagerInternal是一个抽象类,其此处实例是ActivityServiceManager的LocalServiceend noteActivityManagerInternal->ActivityManagerService:startProcessLockedActivityManagerService->ProcessList:startProcessLockedProcessList->ProcessList:startProcessLockedProcessList->ProcessList:startProcessLockedProcessList->ProcessList:startProcessLockedProcessList->ProcessList:startProcessProcessList->Process:startProcess->ZygoteProcess:startZygoteProcess->ZygoteProcess:startViaZygotenote right写入参数,通过socket发送给Zygoteend noteZygoteProcess->ZygoteProcess:zygoteSendArgsAndGetResultZygoteProcess->ZygoteProcess:attemptZygoteSendArgsAndGetResultZygoteProcess->ZygoteState:writenote right正式发送给zygoteend noteZygoteProcess->ZygoteState:readnote right主动读取结果,获取新启动的进程pid,保存到上下文中,等待attach新启动的进程,由此,我们进入Zygote进程end note==Zygote==ZygoteInit->ZygoteInit:mainZygoteInit->ZygoteServer:runSelectLoopZygoteServer->ZygoteConnection:processOneCommandZygoteConnection->Zygote:forkAndSpecializenote right这个返回值很关键,如果在此处将fork出两个进程,如果是新创建的进程,pid为0,否则就是Zygote进程end noteZygote-->ZygoteConnection:pidZygoteConnection->ZygoteConnection:handleParentProcZygoteConnection->ZygoteState:writenote right写入新创建的pid给SystemServiceend noteZygoteConnection-->ZygoteServer:processOneCommand:RunnableZygoteServer->ZygoteServer:runSelectLoopnote right继续监听其它连接,完毕end note==App进程==Zygote-->ZygoteConnection:pidZygoteConnection->ZygoteConnection:handleChildProcZygoteConnection->closeSocketZygoteConnection->ZygoteInit:childZygoteInitZygoteInit->RuntimeInit:findStaticMainnote right这里找到ActivityThread.main()方法end noteRuntimeInit-->ZygoteInit:mainRuntimeInit-->ZygoteConnection:mainZygoteConnection-->ZygoteServer:mainZygoteServer-->ZygoteInit:runSelectLoop:mainZygoteInit->ActivityThread:mainnote left至此,app的入口正式启动,这里负责创建Looper.prepare();还有最重要的attach,毕竟刚创建的app是个新生儿,需要把自己注册到android世界中,才能获得活动的权利最后进入主线程死循环loopend noteActivityThread->ActivityThread:attachActivityThread->ActivityManagerService.Stub:attachApplication==SystemService==ActivityManagerService.Stub->ActivityManagerService:attachApplicationActivityManagerService->ActivityManagerService:attachApplicationLockedActivityManagerService->ActivityTaskManagerInternalnote right这个是ATMS的LocalServiceend noteActivityTaskManagerInternal->RootWindowContainer:attachApplicationRootWindowContainer->RootWindowContainer:startActivityForAttachedApplicationIfNeededRootWindowContainer->ActivityStackSupervisor:realStartActivityLockednote left现在进程有了,该真正启动了end noteActivityStackSupervisor->ClientLifecycleManager:scheduleTransactionnote right这里面约莫842行,给事务添加了回调,LaunchActivityItem;然后也设置的生命周期状态ResumeActivityItemend noteClientLifecycleManager->ClientTransaction:scheduleClientTransaction->IApplicationThread:scheduleTransaction==App进程==IApplicationThread->ActivityThread:scheduleTransactionActivityThread->ActivityThread:sendMessage(159,transaction)ActivityThread->Handler:sendMessageHandler->TransactionExecutor:executeTransactionExecutor->TransactionExecutor:executeCallbacksnote right注意此处的LaunchActivityItemend noteTransactionExecutor->LaunchActivityItem:executeLaunchActivityItem->ActivityThread:handleLaunchActivityActivityThread->ActivityThread:performLaunchActivityActivityThread->Instrumentation:newActivityInstrumentation-->ActivityThread:activityActivityThread->Activity:attachnote left那么,activity就被反射创建了,这里,创建了PhoneWindow等必要成员end noteActivityThread->Instrumentation:callActivityOnCreateInstrumentation->Activity:performCreateActivity->Activity:onCreateActivity-->TransactionExecutorTransactionExecutor->TransactionExecutor:executeLifecycleStateTransactionExecutor->TransactionExecutor:cycleToPathnote left这样终态传入的是Resume,因此需要在此处拿到IntArray的生命周期表end noteTransactionExecutor->TransactionExecutor:performLifecycleSequencenote left挨个走start、resume,end noteTransactionExecutor->ActivityThread:handleStartActivityActivityThread->Activity:performStartActivity->Instrumentation:callActivityOnStartInstrumentation->Activity:onStartActivity-->TransactionExecutorTransactionExecutor->ActivityThread:handleResumeActivityActivityThread->Activity:performResumeActivity->Instrumentation:callActivityOnResumeInstrumentation->Activity:onResumeActivity-->ActivityThread:handleResumeActivitynote left此处,继续走handlerResumeActivity接下来的分支,就是绑定Activity和Window的过程,wm.addView(decor, l);自此就是View绘制流程(极多的另一流程),然后显示在屏幕上end noteActivity-->ActivityThread:mainActivityThread->Looper:loop@enduml