Android 启动Activity面流程(Android 9.0)

Android开发过程中,可以说接触最多的就是启动Activity了,实际上从Launcher打开一个应用就是启动一个Activity了,我们可以从这点出来来梳理下Android启动Activity的具体流程,顺带也理一下ActivivtyManagerService的工作原理。

1、Launcher通知AMS启动一个Activity

Launcher.java

startActivitySafely() 
→ startActivity()

Activity.java

startActivity() 
→ startActivityForResult(mInstrumentation.execStartActivity())

Instrumentation.java

execStartActivity(ActivityManager.getService().startActivity())

这一步中,ActivityManager同过Binder通信(AIDL方式实现)调用起AMS中的startActivity方法

ActivityManagerService.java

startActivity() 
→ startActivityAsUser()

ActivityStarter.java

execute()
 → startActivityMayWait(int res = startActivity())
 → startActivity(mLastStartActivityResult = startActivity()) 
 → startActivity(result = startActivityUnchecked()) 
 → startActivityUnchecked(mSupervisor.resumeFocusedStackTopActivityLocked())

ActivityStackSupervisor.java

resumeFocusedStackTopActivityLocked()

ActivityStack.java

resumeTopActivityUncheckedLocked(result = resumeTopActivityInnerLocked()) 
→ resumeTopActivityInnerLocked(pausing |= startPausingLocked())
→ startPausingLocked()

这一步将开始让Launcher页进入Pausing状态,下一个小节对这里的startPausingLocked方法展开梳理

2、AMS通知Launcher进入Pausing状态

ActivityStack.java

startPausingLocked(mService.getLifecycleManager().scheduleTransaction())

ClientLifecycleManager.java

scheduleTransaction(transactionWithState())
 → scheduleTransaction(transaction.schedule())

ClientTransaction.java

schedule(mClient.scheduleTransaction(this))

mClient是IApplicationThread,ActivityThread中ApplicationThread extends IApplicationThread.Stub,这里我的理解是ActivityThread实际上也是一个Binder Server,这里通过Binder通信调用到ActivityThread中的scheduleTransaction方法

ActivityThread.java

scheduleTransaction(ActivityThread.this.scheduleTransaction())

ClientTransactionHandler.java

scheduleTransaction(sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction))

ActivityThread.java

handleMessage(case EXECUTE_TRANSACTION)

TransactionExecutor.java

execute(executeLifecycleState(transaction))

在executeLifecycleState方法中有以下两个重要的调用分支,这点要注意

分支1:
lifecycleItem.execute(mTransactionHandler, token, mPendingActions) 
→ PauseActivityItem.execute()
→ ClientTransactionHandler.handlePauseActivity()
分支2:
lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions) 
→PauseActivityItem.postExecute(ActivityManager.getService().activityPaused(token))

lifecycleItem是在startPausingLocked方法中传入的PauseActivityItem变量,所以这里的execute以及postExecute都执行到了PauseActivityItem方法中

ActivityThread.java

handlePauseActivity(performPauseActivity()) 
→ performPauseActivityIfNeeded(mInstrumentation.callActivityOnPause())

上面代码是基于分支1的展开

Instrumentation.java

callActivityOnPause(activity.performPause())

Activity.java

performPause() → onPause()

到这里Launcher便进入了Pausing的状态,紧接着前面第二个分支就开始执行,启动新的Activity,我们也由此可见,前一个activity得执行完onPause后新的Activity才开始启动。

3、启动新的Activity

启动新的activiy从分支2开始执行,注意这里代码是执行在Launcher的ActivityThread中的,通过Binder通信调用AMS的activityPaused方法

ActivityManager.getService().activityPaused(token)

ActivityManagerService.java

activityPaused(stack.activityPausedLocked())

ActivityStack.java

activityPausedLocked(completePauseLocked(true, null)) 
→ completePauseLocked(mStackSupervisor.resumeFocusedStackTopActivityLocked())

ActivityStackSupervisor.java

resumeFocusedStackTopActivityLocked(return targetStack.resumeTopActivityUncheckedLocked())

ActivityStack.java

resumeTopActivityUncheckedLocked() 
→ resumeTopActivityInnerLocked(mStackSupervisor.startSpecificActivityLocked(next, true, true))

ActivityStackSupervisor.java

startSpecificActivityLocked(mService.startProcessLocked())

这里的mService持有AMS的引用
ActivityManagerService.java

startProcessLocked() 
→ startProcess(startResult = Process.start())

需要注意的是startProcessLocked方法中关键代码:
final String entryPoint = “android.app.ActivityThread”;
return startProcessLocked(…, entryPoint, …);

Process.java

start(return zygoteProcess.start())

ZygoteProcess.java

start() → startViaZygote() → zygoteSendArgsAndGetResult()

ActivityThread.java

main()

到这里新的App的进程就启动起来了。

4、AMS如何持有新进程的Binder对象

ActivityThread.java

main(thread.attach(false, startSeq)) 
→ attach(mgr.attachApplication(mAppThread, startSeq))

ActivityManagerService.java

attachApplication()
 → attachApplicationLocked(mStackSupervisor.attachApplicationLocked(app))

这里ActivityThread通过Binder通信将一个ApplicationThread类型的Binder对象传递给ActivityManagerService,以便以后ActivityManagerService能够通过这个Binder对象和它进行通信

5、AMS通知ActivityThread启动Activity

ActivityStackSupervisor.java

attachApplicationLocked() 
→realStartActivityLocked(mService.getLifecycleManager().scheduleTransaction(clientTransaction))

ActivityThread.java

scheduleTransaction(ActivityThread.this.scheduleTransaction())

ClientTransactionHandler.java

scheduleTransaction(sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction))

ActivityThread.java

handleMessage(case EXECUTE_TRANSACTION)

TransactionExecutor.java

execute(executeCallbacks(transaction) && executeLifecycleState(transaction))

注意这里又有两个分支
分支1:executeCallbacks()

LaunchActivityItem.execute(ActivityThread.handleLaunchAcitivity(final Activity a = performLaunchActivity(r, customIntent)))

ActivityThread.java

performLaunchActivity(mInstrumentation.callActivityOnCreate())

Instrumentation.java

callActivityOnCreate(activity.performCreate(icicle, persistentState))

Activity.java

performCreate() 
→ onCreate()

分支2:executeLifecycleState()
这里需要说明以下几点
1、 realStartActivityLocked中的scheduleTransaction中的mClient是与系统进程通信的服务端,即ActivityThread.ApplicationThread,即此处调用的是ActivityThread.ApplicationThread.scheduleTransaction
2、 executeCallbacks中的lifecycleItem是realStartActivityLocked中加入的clientTransaction.addCallback(LaunchActivityItem.obtain())
3、 executeLifecycleState中先执行cycleToPath走onStart流程,然后onResume等等,由于本文主要记载activity启动过程AMS的运转,所以到onCreate就行了。
4、 ActivityThread继承了ClientTransactionHandler,将原先放在ActivityThread里面的handleLaunchActivity、handlerStartActivity等等抽取出来,放到ClientTransactionHandler类中,作为抽象方法。

这篇博客目前基本是引用了Android P ActivityManagerService(一)Activity启动代码流程这篇博客的流程,主要是这个流程的东西没啥区别,而这篇博客的确比较详细,而且我验证过这个流程没有问题。我重新在这里梳理一次的目的是在这个流程基础上做详细说明,当然现在还没弄,后面找到时间再来好好说说

你可能感兴趣的:(Android)