再谈启动一个Activity大致时序图

太多了,笔者不想写,

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

补充说明下,Android31版本。

@startuml
'https://plantuml.com/sequence-diagram
skinparam dpi 800
scale 15000 width
scale 5000 height

autonumber
==Launcher==
Launcher -> Activity:startActivity(intent)
Activity->Activity:startActivity(intent,bundle)
Activity->Activity:startActivityForResult
Activity->Instrumentation:execStartActivity
note left
activity通过调用内部成员
mInstrumentation与atms
交互,即将开始进入系统进程
end note
Instrumentation->ActivityTaskManagerService.Stub:startActivity
==SystemService==
ActivityTaskManagerService.Stub->ActivityTaskManagerService:StartActivity
ActivityTaskManagerService->ActivityTaskManagerService:startActivityAsUser
ActivityTaskManagerService->ActivityTaskManagerService:startActivityAsUser
ActivityTaskManagerService->ActivityStarter:execute
ActivityStarter->ActivityStarter:executeRequest
note left
这里,会验证启动参数的合法性,
随后,创建一个ActivityRecord
作为一个Activity的唯一存在标识
end note
ActivityStarter->ActivityStarter:startActivityUnchecked
ActivityStarter->ActivityStarter:startActivityInner
note left
此函数的功能,如果没有Stack,就要新创建一个Stack,
也就是ActivityStack,保存到mTargetTask中,
通过setNewTask将待启动的ActivityRecord
和新创建的Stack绑定,并且将其置于Stack Top,
see ActivityStack::startActivityLocked()
see ActivityStack::moveToFront()
end note
ActivityStarter->ActivityStack:startActivityLocked
note left
这就是绑定ActivityRecord和新创建的Stack(Task)
end note

ActivityStack->RootWindowContainer:resumeFocusedStacksTopActivities
note left
对于新启动的Activity,走此分支
end note

RootWindowContainer->ActivityStack:resumeTopActivityUncheckedLocked
note left
此ActivityStack是13步新创建的,
开始调度Activity
end note

ActivityStack->ActivityStack:resumeTopActivityInnerLocked

note left
这一步很关键,因为要Resume即将启动的Activity,
那么,首先要检查即将启动的Activity在没在栈顶,
如果不在,就要去pause栈顶的Activity
end note
ActivityStack->TaskDisplayArea:pauseBackStacks
note left
这里返回是否在pause栈顶的Activity,
一般这里会返回true,因为需要pause Launcher,
传入next参数即topRunningActivity
end note

note right
逻辑是去topStack中去判断是否有可见Activity,
如果有则pause掉,否则返回false,
end note
TaskDisplayArea->ActivityStack:startPausingLocked
note right
开始pause Launcher,
通过mAtmService(ATMS)获取生命周期管理,
执行生命周期PauseActivityItem事务
end note
ActivityStack->ClientLifecycleManager:scheduleTransaction

note left
注意,这里传的参数有PauseActivityItem
end note
ClientLifecycleManager->ClientLifecycleManager:scheduleTransaction
ClientLifecycleManager->ClientTransaction:schedule
ClientTransaction->IApplicationThread:scheduleTransaction
note right
进入Launcher进程,去执行Pause事务
end note

TaskDisplayArea-->ActivityStack:pauseBackStacks:true
ActivityStack-->RootWindowContainer:resumeTopActivityInnerLocked:true
RootWindowContainer-->ActivityStarter:resumeTopActivityUncheckedLocked:true
ActivityStarter-->ActivityStarter:ActivityStarter:START_SUCCESS


==Launcher进程==
IApplicationThread->ActivityThread:scheduleTransaction
ActivityThread->ActivityThread:sendMessage(159,transaction)
ActivityThread->Handler:sendMessage
note right
这里159代表事务EXECUTE_TRANSACTION
一顿Looper后可以直接看handleMessage处
end note

Handler->Handler:handleMessage
ActivityThread->TransactionExecutor:execute

TransactionExecutor->TransactionExecutor:executeLifecycleState
TransactionExecutor->PauseActivityItem:execute
PauseActivityItem->ActivityThread:handlePauseActivity
note right
熟悉的ActivityThread,开始pause Activity
end note

ActivityThread->ActivityThread:performPauseActivity
ActivityThread->ActivityThread:performPauseActivityIfNeeded
ActivityThread->Instrumentation:callActivityOnPause
Instrumentation->Activity:performPause
Activity->Activity:onPause

TransactionExecutor->PauseActivityItem:postExecute
PauseActivityItem->ActivityTaskManagerService.Stub:activityPaused
note left
通知系统进程,Launcher已经pause
end note
==SystemService==
ActivityTaskManagerService->ActivityRecord:activityPaused(false/*timeout*/)
note right
拓展:这里就是其中一种ANR,如果没超时就移除anr回调,
end note
ActivityRecord->ActivityStack:completePauseLocked(true/*resumeNext*/,null)
ActivityStack->RootWindowContainer:resumeFocusedStacksTopActivities

RootWindowContainer->ActivityStack:resumeTopActivityUncheckedLocked

note right
找到下一个即将启动的ActivityStack,开始Resume
end note

ActivityStack->ActivityStack:resumeTopActivityInnerLocked

ActivityStack->TaskDisplayArea:pauseBackStacks
TaskDisplayArea-->ActivityStack:pauseBackStacks:false

ActivityStack->ActivityStackSupervisor:startSpecificActivity
note right
这里通过ActivityRecord去获取WindowProcessController,
这个和进程相关,第一次启动肯定没有对应进程,那么就需要创建进程
end note

ActivityStackSupervisor->ActivityTaskManagerService:startProcessAsync
ActivityTaskManagerService->ActivityManagerInternal:startProcess
note right
这里通过handler调用到此处,
ActivityManagerInternal是一个抽象类,
其此处实例是ActivityServiceManager的LocalService
end note
ActivityManagerInternal->ActivityManagerService:startProcessLocked
ActivityManagerService->ProcessList:startProcessLocked
ProcessList->ProcessList:startProcessLocked
ProcessList->ProcessList:startProcessLocked
ProcessList->ProcessList:startProcessLocked
ProcessList->ProcessList:startProcess
ProcessList->Process:start
Process->ZygoteProcess:start
ZygoteProcess->ZygoteProcess:startViaZygote
note right
写入参数,通过socket发送给Zygote
end note
ZygoteProcess->ZygoteProcess:zygoteSendArgsAndGetResult
ZygoteProcess->ZygoteProcess:attemptZygoteSendArgsAndGetResult
ZygoteProcess->ZygoteState:write
note right
正式发送给zygote
end note
ZygoteProcess->ZygoteState:read
note right
主动读取结果,获取新启动的进程pid,保存到上下文中,
等待attach新启动的进程,由此,我们进入Zygote进程
end note

==Zygote==
ZygoteInit->ZygoteInit:main
ZygoteInit->ZygoteServer:runSelectLoop
ZygoteServer->ZygoteConnection:processOneCommand
ZygoteConnection->Zygote:forkAndSpecialize
note right
这个返回值很关键,如果在此处将fork出两个进程,
如果是新创建的进程,pid为0,否则就是Zygote进程
end note

Zygote-->ZygoteConnection:pid
ZygoteConnection->ZygoteConnection:handleParentProc
ZygoteConnection->ZygoteState:write
note right
写入新创建的pid给SystemService
end note
ZygoteConnection-->ZygoteServer:processOneCommand:Runnable
ZygoteServer->ZygoteServer:runSelectLoop
note right
继续监听其它连接,完毕
end note
==App进程==
Zygote-->ZygoteConnection:pid
ZygoteConnection->ZygoteConnection:handleChildProc
ZygoteConnection->closeSocket
ZygoteConnection->ZygoteInit:childZygoteInit
ZygoteInit->RuntimeInit:findStaticMain
note right
这里找到ActivityThread.main()方法
end note
RuntimeInit-->ZygoteInit:main
RuntimeInit-->ZygoteConnection:main
ZygoteConnection-->ZygoteServer:main
ZygoteServer-->ZygoteInit:runSelectLoop:main
ZygoteInit->ActivityThread:main
note left
至此,app的入口正式启动,
这里负责创建Looper.prepare();
还有最重要的attach,毕竟刚创建的app是个新生儿,
需要把自己注册到android世界中,才能获得活动的权利
最后进入主线程死循环loop
end note
ActivityThread->ActivityThread:attach
ActivityThread->ActivityManagerService.Stub:attachApplication
==SystemService==
ActivityManagerService.Stub->ActivityManagerService:attachApplication
ActivityManagerService->ActivityManagerService:attachApplicationLocked
ActivityManagerService->ActivityTaskManagerInternal
note right
这个是ATMS的LocalService
end note
ActivityTaskManagerInternal->RootWindowContainer:attachApplication
RootWindowContainer->RootWindowContainer:startActivityForAttachedApplicationIfNeeded
RootWindowContainer->ActivityStackSupervisor:realStartActivityLocked
note left
现在进程有了,该真正启动了
end note
ActivityStackSupervisor->ClientLifecycleManager:scheduleTransaction
note right
这里面约莫842行,给事务添加了回调,LaunchActivityItem;
然后也设置的生命周期状态ResumeActivityItem
end note
ClientLifecycleManager->ClientTransaction:schedule
ClientTransaction->IApplicationThread:scheduleTransaction
==App进程==
IApplicationThread->ActivityThread:scheduleTransaction
ActivityThread->ActivityThread:sendMessage(159,transaction)
ActivityThread->Handler:sendMessage
Handler->TransactionExecutor:execute
TransactionExecutor->TransactionExecutor:executeCallbacks
note right
注意此处的LaunchActivityItem
end note
TransactionExecutor->LaunchActivityItem:execute
LaunchActivityItem->ActivityThread:handleLaunchActivity
ActivityThread->ActivityThread:performLaunchActivity
ActivityThread->Instrumentation:newActivity
Instrumentation-->ActivityThread:activity
ActivityThread->Activity:attach

note left
那么,activity就被反射创建了,
这里,创建了PhoneWindow等必要成员
end note
ActivityThread->Instrumentation:callActivityOnCreate
Instrumentation->Activity:performCreate
Activity->Activity:onCreate
Activity-->TransactionExecutor
TransactionExecutor->TransactionExecutor:executeLifecycleState
TransactionExecutor->TransactionExecutor:cycleToPath
note left
这样终态传入的是Resume,因此需要在此处拿到IntArray的生命周期表
end note
TransactionExecutor->TransactionExecutor:performLifecycleSequence
note left
挨个走start、resume,
end note
TransactionExecutor->ActivityThread:handleStartActivity
ActivityThread->Activity:performStart
Activity->Instrumentation:callActivityOnStart
Instrumentation->Activity:onStart
Activity-->TransactionExecutor

TransactionExecutor->ActivityThread:handleResumeActivity
ActivityThread->Activity:performResume
Activity->Instrumentation:callActivityOnResume
Instrumentation->Activity:onResume
Activity-->ActivityThread:handleResumeActivity
note left
此处,继续走handlerResumeActivity接下来的分支,
就是绑定Activity和Window的过程,
wm.addView(decor, l);
自此就是View绘制流程(极多的另一流程),然后显示在屏幕上
end note

Activity-->ActivityThread:main
ActivityThread->Looper:loop
@enduml

再谈启动一个Activity大致时序图_第1张图片

你可能感兴趣的:(android)