Activity启动流程源码分析

基于Android Q的版本

  • Activity.startActivity(有几个重载方法)
  • Activity.startActivityForResult
  • Instrumentation.execStartActivity
  • ActivityTaskManager.getService().startActivity
    即 IActivityTaskManagerSingleton.get().startActivity
    即 IActivityTaskManager.startActivity
    (aidl实现binder跨进程通讯,通讯的客户端,以上部分在应用进程)
  • ActivityTaskManagerService.startActivity
    (ActivityTaskManagerService 继承 IActivityTaskManager.Stub,实现了中的startActivity startActivities startActivityAsUser等方法,是跨进程通讯的服务端,这部分在系统进程)
  • ActivityTaskManagerService.startActivityAsUser
  • ActivityStarter.execute
  • ActivityStarter.startActivityMayWait
  • ActivityStarter.startActivity(有几个重载方法)
  • ActivityStarter.startActivityUnchecked
  • ActivityStack.ensureActivitiesVisibleLocked(有几个重载方法)
  • ActivityRecord.makeClientVisible
  • ActivityRecord.makeActiveIfNeeded
  • ActivityStack.resumeTopActivityUncheckedLocked
  • ActivityStack.resumeTopActivityInnerLocked
  • ActivityStackSupervisor.startSpecificActivityLocked
  • ActivityStackSupervisor.realStartActivityLocked
  • mService.getLifecycleManager().scheduleTransaction(clientTransaction)
    即 ClientLifecycleManager.scheduleTransaction
  • ClientTransaction.schedule
    (ClientTransaction中的mActivityCallbacks就是LaunchActivityItem)
  • mClient.scheduleTransaction()
    即 IApplicationThread.scheduleTransaction
    (这里也是使用aidl通过binder通讯,这是通讯的客户端,这部分在系统进程)
  • ApplicationThread.scheduleTransaction
    (注:1.ApplicationThread是ActivityThread的内部类)
    (注:2.ApplicationThread 继承了 IApplicationThread.Stub,实现了scheduleTransaction等方法,跨进程通讯的服务端,这部分在应用进程)
  • ActivityThread.scheduleTransaction
  • ClientTransactionHandler.scheduleTransaction
    (ActivityThread 继承了 ClientTransactionHandler,此处调用父类方法)
  • ActivityThread.sendMessage (what = EXECUTE_TRANSACTION)
  • ActivityThread$H.handleMessage
    (使用ActivityThread内部Handler类H的handleMessage方法接收)
  • TransactionExecutor.execute
  • TransactionExecutor.executeCallbacks
  • ClientTransactionItem.execute
    (ClientTransactionItem 就是 LaunchActivityItem 的父类)
  • ClientTransactionHandler.handleLaunchActivity
    (ClientTransactionHandler 是 ActivityThread 的父类)
  • ActivityThread.performLaunchActivity
    {Instrumentation.newActivity创建activity实例 -> activity.attach -> activity.mIntent = customIntent -> activity.setTheme}
  • Instrumentation.callActivityOnCreate
  • Activity.performCreate
  • Activity.onCreate
    到这里activity就被启动起来了。

总体来说,不管是哪个版本都是:

  • 首先startActivity
  • 通过Instrumentation调用ActivityManagerService(简称AMS)通过跨进程通讯,从应用进程调用到系统进程,并在系统进程中进行一些列操作
  • 在系统进程处理完成后,通过IApplicationThread也进行跨进程通讯,从系统进程回到应用进程,通过ActivityThread中的Handler处理消息
  • 最后又通过Instrumentation创建要启动的Activity,并调用创建的activity的onCreate方法
    也就是说启动Activity其实是经过了两次跨进程通讯才将Activity启动起来的。

感谢 https://www.jianshu.com/p/d94a40e0e5ec

你可能感兴趣的:(Activity启动流程源码分析)