Activity 的启动流程

参考 : https://juejin.cn/post/6844903897748733966

流程如下 :

  1. Activity 对象的 startActivity(intent) 方法

  2. Activity 对象的 startActivityForResult(intent) 方法

  3. 调用 mInstrumentation.execStartActivity()

  4. 调用 ActivityManager.getService().startActivity() 方法, 其中 ActivityManager.getService() 返回的是 ActivityManagerService (AMS) 在应用进程的本地代理。

  5. 调用 AMS 的 startActivity() 方法

  6. 调用 ActivityStarter 的 startActivity() 方法

  7. 调用 ApplicationThread 的 scheduleLaunchActivity() 方法, 该方法中发送一个 H.LAUNCH_ACTIVITY 消息

  8. ActivityThread 的 Handler 的 handleMessage() 方法处理 H.LAUNCH_ACTIVITY 消息, 调用 handleLaunchActivity() 方法

    • 调用 ActivityThread 的 performLaunchActivity() 方法
      1. 创建 Activity 实例
      2. 执行 onCreate()onStart() 生命周期
    • 调用 ActivityThread 的 handleResumeActivity() 方法 ()
      1. 执行 onResume() 生命周期
      2. 为 WindowManager 添加 DecorView
  9. ActivityThread 的 handleResumeActivity() 方法最终会调用 ViewRootImpl 的requestLayout() 方法

  10. 调用 ViewRootImpl 的 scheduleTraversals() 方法

  11. 调用 ViewRootImpl 的 performTraversals() 方法

  12. 调用 ViewRootImpl 的 performMeasure(),performLayout(), performDraw() 方法

简化版 :

  1. Activity 对象的 startActivity(intent) 方法
  2. 调用 ActivityManagerService (AMS) 的 startActivity() 方法
  3. ActivityThread 的 handleResumeActivity() 方法
  4. ViewRootImpl 的 requestLayout() 方法
  5. ViewRootImpl 的 performTraversals() 方法
  6. ViewRootImpl 的 performMeasure(),performLayout(), performDraw()

你可能感兴趣的:(Activity 的启动流程)