一、流程
Activity.startActivity [这里中间会经过多个Activity流程]
->Activity.startActivityForResult
->Instrumentation.execStartActivity
->ActivityTaskManagerService.startActivity [这是通过ActivityTaskManager.getService()]
->ActivityTaskManagerService.startActivityAsUser
->ActivityStartController.obtainStarter [这是为了获取ActivityStarter]
->ActivityStarter.execute [setMayWait设置mRequest.mayWait为true]
->ActivityStarter.startActivityMayWait [初始化rInfo和aInfo]
->ActivityStarter.startActivity
->ActivityStarter.startActivity [初始化err和ActivityRecord]
->ActivityStarter.startActivity
->ActivityStarter.startActivityUnchecked [初始化TaskRecord]
->RootActivityContainer.resumeFocusedStacksTopActivities
->ActivityStack.resumeTopActivityUncheckedLocked
->ActivityStack.resumeTopActivityInnerLocked
->ActivityStack.startPausingLocked [开始暂停当前恢复的活动]
->PauseActivityItem.obtain
->ActivityTaskManagerService.getLifecycleManager
->ClientLifecycleManager.scheduleTransaction
->ClientLifecycleManager.scheduleTransaction
->ClientTransaction.getClient
->ClientTransaction.schedule
->IApplicationThread.scheduleTransaction
->ActivityThread.scheduleTransaction
->ClientTransactionHandler.scheduleTransaction
->ActivityThread.sendMessage [H.EXECUTE_TRANSACTION]
->TransactionExecutor.execute
->TransactionExecutor.executeCallbacks [这个流程的item是PauseActivityItem]
->TransactionExecutor.cycleToPath
->TransactionExecutor.performLifecycleSequence
->PauseActivityItem.execute
->ActivityThread.handlePauseActivity
->ActivityThread.performPauseActivity
->ActivityThread.performPauseActivityIfNeeded
->Instrumentation.callActivityOnPause
->Activity.performPause
->Activity.onPause
->PauseActivityItem.postExecute
->ActivityTaskManagerService.activityPaused
->ActivityStack.activityPausedLocked [ActivityStack对象是在ActivityRecord]
->ActivityStack.completePauseLocked
->RootActivityContainer.resumeFocusedStacksTopActivities
->ActivityStack.resumeTopActivityUncheckedLocked
->ActivityStack.resumeTopActivityInnerLocked
->ActivityStack.resumeTopActivityInnerLocked [暂停流程结束又回到了这个方法]
->ActivityStackSupervisor.startSpecificActivityLocked [分两种情况,没有应用进程和有]
->ActivityStackSupervisor.realStartActivityLocked [有应用程序进程]
->ActivityTaskManagerService.mH.sendMessage [没有应用程序进程]
这里说明下:sendMessage其实是发送了一个Function,即ActivityManagerInternal::startProcess。下面就是分析startProcess
->ActivityManagerInternal.startProcess [AMS.LocalService.startProcess]
->ActivityManagerService.LocalService.startProcess
->ActivityManagerService.startProcessLocked
->ProcessList.startProcessLocked
->ProcessList.startProcessLocked [三个参数]
->ProcessList.startProcessLocked [五个参数]
->ProcessList.startProcessLocked [十二个参数]
->ProcessList.startProcess
->Process.start [非zygote进程]
->ZygoteProcess.start
->ZygoteProcess.startViaZygote
->ZygoteProcess.openZygoteSocketIfNeeded [这是由下一个方法调用]
->ZygoteProcess.attemptConnectionToPrimaryZygote [尝试连接Zygote]
->ZygoteState.connect
->ZygoteProcess.zygoteSendArgsAndGetResult
->ZygoteProcess.attemptZygoteSendArgsAndGetResult
->zygoteState.mZygoteOutputWriter.write(msgStr) [localsocket通信]
->执行到这里,就已经发送了Socket给Zygote进程,下面分析Zygote进程启动
->app_main.cpp的main方法
->AndroidRuntime.start
->ZygoteInit.main [app启动Zygote进程]
->new ZygoteServer()
->zygoteServer.runSelectLoop(abiList) [循环监听AMS发来的消息]
->ZygoteServer.acceptCommandPeer [pollIndex == 0]
->ZygoteSocket.accept()
->ZygoteServer.createNewConnection
->new ZygoteConnection
->ZygoteConnection.processOneCommand [pollIndex < usapPoolEventFDIndex]
->Zygote.forkAndSpecialize [这里就是fork流程]
->Zygote.nativeForkAndSpecialize
->com_android_internal_os_Zygote.cpp#nativeForkAndSpecialize
->com_android_internal_os_Zygote.cpp#ForkAndSpecializeCommon
->fork 不再往底层分析 processOneCommand
->ZygoteConnection.handleChildProc [parsedArgs.mInvokeWith=null]
->ZygoteInit.zygoteInit [isZygote=false]
->RuntimeInit.redirectLogStreams()
->RuntimeInit.commonInit()
->ZygoteInit.nativeZygoteInit() [在AndroidRuntime里面注册]
->com_android_internal_os_ZygoteInit_nativeZygoteInit
->gCurRuntime->onZygoteInit #具体实现在AppRuntime即app_main里面
->ProcessState::self->startThreadPool #开启binder线程池
->RuntimeInit.applicationInit [回到ZygoteInit]
->RuntimeInit.findStaticMain
->new MethodAndArgsCaller [最终返回给runSelectLoop]
->MethodAndArgsCaller.run [在ZygoteInit.main取到runnable执行]
->Method.invoke [在MethodAndArgsCaller.run中]
->ActivityThread.main [传到MethodAndArgsCaller中]
->new ActivityThread
->ActivityThread.attach
->ActivityManagerService.attachApplication [传入IApplicationThread这个Binder]
->ActivityManagerService.attachApplicationLocked [处理attachApplicationLocked]
->ApplicationThread.bindApplication [绑定AMS这个binder]
->ActivityThread.sendMessage [发送BIND_APPLICATION]
->ActivityThread.handleBindApplication
->ActivityThread.getPackageInfoNoCheck [获取LoadedApk]
->ContextImpl.createAppContext
->反射创建Instrumentation
->LoadedApk.makeApplication
->Instrumentation.newApplication
->Application.attach
->Instrumentation.callApplicationOnCreate
->ActivityManagerService.attachApplicationLocked [回到attachApplicationLocked]
->AtmInternal.attachApplication [ActivityTaskManagerService]
->ActivityStackSupervisor.realStartActivityLocked [回到了启动Activity的流程]
->通过ClientTransaction启动Activity
二、参考
Android10.0应用进程创建过程以及Zygote的fork流程-[Android取经之路]
在Android 10.0中Activity的启动流程