Android10和Android12的Activity启动流程整理
回顾一下Android10的Activity的启动流程:
- Activity # startActivity
- Activity # startActivityForResult
- Instrumentation # execStartActivity // 在Android10之前,是交给AMS进行处理,随后交给ATMS处理,而Android10之后,直接交给ATMS进行处理
- ActivityTaskManagerService # startActivity
- ActivityTaskManagerService # startActivityAsUser
- ActivityStarter # execute
- ActivityStarter # startActivityMayWait
- ActivityStarter # startActivity
- ActivityStarter # startActivityUnchecked
- ActivityStack # startActivityLocked
- ActivityStack # ensureActivitiesVisibleLocked
- ActivityRecord # makeClientVisible
- ActivityRecord # makeActiveIfNeeded
- ActivityStack # resumeTopActivityUncheckedLocked
- ActivityStack # resumeTopActivityInnerLocked
- ActivityStackSupervisor # startSpecificActivityLocked
- ActivityStackSupervisor # realStartActivityLocked
- ClientLifecycleManager # scheduleTransaction 在这里会去获取ActivityTaskManagerService的LifecycleManager对象,去执行事务
- ClientTransaction # schedule()
- IApplicationThread(ApplicationThread为该aidl接口的实现类) # scheduleTransaction
- ActivityThread(ClientTransactionHandler为ActivityThread的父类,交由其父类处理) # scheduleTransaction
- ClientTransactionHandler # scheduleTransaction 该方法会发送一个Message ActivityThread.H.EXECUTE_TRANSACTION
- ActivityThread # handleMessage 接着交由TransactionExecutor进行处理
- TransactionExecutor # execute() 交由TransactionExecutor处理事务
- TransactionExecutor # executeCallbacks 向注册的监听回调对应的状态
- ClientTransactionItem # execute 在这里是LaunchActivityItem的类型,因为在ActivityStackSupervisor进行封装Transaction的时候,封装的是LaunchActivityItem类型的事务对象,所以这里从事务对象中获取时,也是执行LaunchActivityItem的execute方法
- ClientTransactionHandler(ActivityThread) # handleLaunchActivity
- ActivityThread # performLaunchActivity 这里就是启动Activity的核心位置
- Instrumentation # callActivityOnCreate
- Activity # performCreate
- Activity # onCreate
Android10的启动Activity的代码就到这里,接着就会回调到开始启动Activity的onCreate()方法
Android12 Activity启动流程
- Activity # startActivity
- Activity # startActivityForResult
- Instrumentation # execStartActivity
- ActivityTaskManagerService # startActivity
- ActivityTaskManagerService # startActivityAsUser
- ActivityStarter # execute
- ActivityStarter # executeRequest
- ActivityStarter # startActivityUnchecked 在这里进行一些检查
- ActivityStarter # startActivityInner 在startActivityInner()的时候,基本上已经检查完毕了,会先去进行状态的初始化,也就是从AcitivityRecord当中将对应的参数存储到本地使用等。
- Task # startActivityLocked 在这里开始,启动Activity的任务就交给了Task处理,不再交给ActivityStack进行处理了
- Task # ensureActivitiesVisible
- Task # forAllLeafTasks(task -> task.mEnsureActivitiesVisibleHelper.process) 在Android12上,采用的大量的Consumer的方式进行事件的处理
- EnsureActivitiesVisibleHelper # process
- EnsureActivitiesVisibleHelper # setActivityVisibilityState
- ActivityRecord # makeActiveIfNeeded
- Task # resumeTopActivityUncheckedLocked 通过ActivityRecord getRootTask来进行后续处理
- Task # resumeTopActivityInnerLocked
- ActivityTaskSupervisor # startSpecificActivity
- ActivityTaskSupervisor # realStartActivityLocked 在这里会进行Transaction的封装,封装了LaunchActivityItem类型的Transaction
- ClientLifecycleManager # scheduleTransaction
- ClientTransaction # schedule()
- IApplicationThread(ApplicationThread为该aidl接口的实现类) # scheduleTransaction
- ActivityThread(ClientTransactionHandler为ActivityThread的父类,交由其父类处理) # scheduleTransaction
- ClientTransactionHandler # scheduleTransaction 该方法会发送一个Message ActivityThread.H.EXECUTE_TRANSACTION
- ActivityThread # handleMessage 接着交由TransactionExecutor进行处理
- TransactionExecutor # execute() 交由TransactionExecutor处理事务
- TransactionExecutor # executeCallbacks 向注册的监听回调对应的状态
- LaunchActivityItem # execute
- ClientTransactionHandler(ActivityThread) # handleLaunchActivity
- ActivityThread # performLaunchActivity
- Instrumentation # callActivityOnCreate
- Activity # performCreate
- Activity # onCreate
暂时通过方法名进行记录,抽空补充流程图。
简单小结一下:
这里是对startActivity的流程进行了简单数理,其实大体上的差异变化没有太大,唯一有变化的就是在Android10上,很多事务Transaction的封装都是通过ActivityStack 和ActivityStackSupervisior来进行处理的。而Android12 是交给了Task和ActivityTaskSupervisor来进行处理,并且移除了ActivityStack和ActivityStackSupervisor这两个类。
参考链接:
https://www.jianshu.com/p/73b748535675
http://aospxref.com/android-12.0.0_r3
https://aospxref.com/android-10.0.0_r47