Activity 启动流程

向ActivityManagerService发起新建Activity的请求

           +---------+    +---------------+   +---------------------+  +--------------------+
           | Context |    |Instrumentation|   |ActivityManagerNative|  |ActivityManagerProxy|
           +----+----+    +-------+-------+   +----------+----------+  +----------+---------+
                |                 |                      |                        |
                |                 |                      |                        |
 startAcvitity  |                 |                      |                        |
 +------------> |                 |                      |                        |
                |execStartActivity|                      |                        |
                +---------------> |                      |                        |
                |                 |      getDefault      |                        |
                |                 +--------------------> |                        |
                |                 |                      |                        |
                |                 | <--------------------+                        |
                |                 |                      |                        |
                |                 |                      |                        |
                |                 |                      |     startActivity      |
                |                 +---------------------------------------------> |
                |                 |                      |                        |
                |                 |                      |                        | transact
                |                 |                      |                        | (START_ACTIVITY_TRANSACTION)
                |                 |                      |                        +---------------------------->
                |                 |                      |                        |         +-------+
                |                 |                      |                        |         |mRemote|
                |                 |                      |                        |         +-------+
                |                 |                      |                        |
                |                 |                      |                        |
                +                 +                      +                        +

ActivityManagerService调用栈顶的Activity的onPause方法

      +----------------------+    +-------------+                                  +----------------+
      |ActivityManagerService|    |ActivityStack|                                  | PackageManager |
      +----------+-----------+    +-----+-------+                                  +--------+-------+
                 |                      |                                                   |
 startActivity   |                      |                                                   |
+--------------> |                      |                                                   |
                 | startActivityMayWaitLock                                                 |
                 +--------------------> |                                                   |
                 |                      |             resolveIntent                         |
                 |                      +-------------------------------------------------> |
                 |                      |                                                   |
                 |                      | <-------------------------------------------------+
                 |                      |                                                   |
                 |                      |         startActivityLocked                       |
                 |                      +-----------------------------------+               |
                 |                      |                                   |               |
                 |                      |                                   |               |
                 | getRecordForAppLocked| <---------------------------------+               +
                 | <--------------------+
                 |                      |
                 +--------------------> |
                 |                      |
                 |                      |    startActivityUncheckedLocked
                 |                      +-----------------------------------+
                 |                      |                                   |
                 |                      | <---------------------------------+
                 |                      |
                 |                      |          startActivityLocked
                 |                      +-----------------------------------+
                 |                      |                                   |
                 |                      | <---------------------------------+
                 |                      |
                 |                      |         resumeTopActivityLock
                 |                      +-----------------------------------+
                 |                      |                                   |
                 |                      | <---------------------------------+
                 |                      |
                 |                      |        startPausingLocked
                 |                      +-----------------------------------+
                 |                      |                                   |
                 |                      | <---------------------------------+
                 |                      |                                          +----------------------+
                 |                      |                                          |ApplicationThreadProxy|
                 |                      |                                          +-----------+----------+
                 |                      |                                                      |
                 |                      |      pre.app.thread.schedulePauseActivity            |
                 |                      +----------------------------------------------------> |
                 |                      |                                                      |
                 |                      |                                                      |
                 |                      |        PAUSE_TIMEOUT_MSG                             |
                 |                      +----+---------------------+                           |
                 |                      |    v                     |                           | transact
                 |                      |    +---------------------+                           | (SCHEDULE_PAUSE_ACTIVITY_TRANSACTION)
                 +                      +                                                      +---------------------------->
                                                                                               |         +-------+
                                                                                               |         |mRemote|
                                                                                               |         +-------+
                                                                                               |
                                                                                               |
                                                                                               |
                                                                                               |
                                                                                               |
                                                                                               +

                                +--------------+                               +---------------------+
                               |ActivityThread|                               | ActitityManagerProxy|
                               +------+-------+                               +-----------+---------+
                                      |                                                   |
                                      |                                                   |
                                      |                                                   |
schedulePauseActivity                 |                                                   |
(SCHEDULE_PAUSE_ACTIVITY_TRANSACTION) |                                                   |
------------------------------------> |                                                   |
                                      |                                                   |
                                      |       queueOrSendMessage                          |
                                      +-----------------------------+                     |
                                      |                             |                     |
                                      | <---------------------------+                     |
                                      |      PAUSE_ACTIVITY                               |
                                      |                                                   |
                                      |                                                   |
                                      |     handlePauseActivity                           |
                                      +-----------------------------+                     |
                                      |                             |                     |
                                      | <---------------------------+                     |
                                      |                                                   |
                                      |     performPauseActivity                          |
                                      +-----------------------------+                     |
                                      |                             |                     |
                                      | <---------------------------+                     |
                                      |                                                   |
                                      |                                                   |
                                      |                                                   |
                                      |               activityPause                       |
                                      +------------------------------------------------>  |
                                      |                                                   | transact
                                      +                                                   |  (ACTIVITY_PAUSED_TRANSACTION)
                                                                                          +---------------------------->
                                                                                          |         +-------+
                                                                                          |         |mRemote|
                                                                                          |         +-------+
                                                                                          |
                                                                                          |
                                                                                          |
                                                                                          +

ActivityManagerService新建一个ActivityThread

    +----------------------+                            +---------------+
    |ActivityManagerService|                            | ActivityStack |
    +----------+-----------+                            +-------+-------+
               |                                                |
               |                                                |
               |                                                |
activityPause  |                                                |
+------------> |                                                |
               |                                                |
               |     activityPause                              |
               +----------------------------------------------> |
               |                                                |
               |                                                |  completePauseLocked
               |                                                +---------------------------+
               |                                                |                           |
               |                                                | <-------------------------+
               |                                                |
               |                                                |   resumeTopActivityLocked
               |                                                +---------------------------+
               |                                                |                           |
               |                                                | <-------------------------+
               |                                                |
               |                                                |   startSpecificActivity
               |                                                +-------------------------+
               |                                                |                         |
               |                                                | <-----------------------+
               |                 startProcessLocked             |
               | <----------------------------------------------+
               |                                                |
               |                                                |
               |  getProcessRecordLocked                        |
               +-------------------------+                      |
               |                         |                      |
               | <-----------------------+                      |
               |                                                |
               |   startProcessLocked                           |
               +-------------------------+                      |
               |                         |                      |
               | <-----------------------+                      |
               |                                                |
               |                                                |             +--------------+       +-------+
               |                                                |             |PackageManager|       |Process|
               |                                                |             +------+-------+       +---+---+
               |                                                |                    |                   |
               |                                                |                    |                   |
               |           getPackageGids                       |                    |                   |
               +-------------------------------------------------------------------> |                   |
               |                                                |                    |                   |
               |                                                |                    |                   |
               |           start                                |                    |                   |
               +---------------------------------------------------------------------------------------> |
               |                                                |                    |                   |  "android.app.ActivityThread"
               |                                                |                    |                   +------------------------------->
               |                                                |                    |                   |
               |                                                |                    |                   |

将新建的ActivityThread attach到应用的Application中(提供上下文)

  +----------------+             +---------------------+
  | ActivityThread |             |ActivityManagerNative|
  +-------+--------+             +----------+----------+
          |                                 |
 main     |                                 |
+-------> |                                 |
          |                                 |
          | attach                          |
          +--------+                        |
          |        |                        |
          | <------+                        |
          |                                 |
          |      attachApplication          |
          +-------------------------------> |
          |                                 |
          |                                 |
          |                                 | transact
          |                                 |  (ATTACH_APPLICATION_TRANSACTION)
          |                                 +---------------------------->
          |                                 |         +-------+
          |                                 |         |mRemote|
          |                                 |         +-------+
          |                                 |
          |                                 |
          +                                 +

      +------------------------+                 +-------------+               +----------------------+
      | ActivityManagerService |                 |ActivityStack|               |ApplicationThreadProxy|
      +------------+-----------+                 +------+------+               +-----------+----------+
                   |                                    |                                  |
                   |                                    |                                  |
                   |                                    |                                  |
attachApplication  |                                    |                                  |
+----------------> |                                    |                                  |
                   |                                    |                                  |
                   | attachApplicationLocked            |                                  |
                   +-------------------------+          |                                  |
                   |                         |          |                                  |
                   | <-----------------------+          |                                  |
                   |                                    |                                  |
                   |                                    |                                  |
                   |  topRunningActivityLocked          |                                  |
                   +----------------------------------> |                                  |
                   |                                    |                                  |
                   |  realStartActivityLocked           |                                  |
                   +----------------------------------> |                                  |
                   |                                    |    scheduleLaunchActivity        |
                   |                                    +--------------------------------> |
                   |                                    |                                  | transact
                   |                                    |                                  |  (SCHEDULE_LAUNCH_ACTIVITY_TRANSACTION)
                   |                                    |                                  +---------------------------->
                   |                                    |                                  |         +-------+
                   +                                    +                                  +         |mRemote|
                                                                                                     +-------+

ApplicationThread新建Activity对象,并调用其生命周期方法

           +-------------------+                         +---------------+           +-------+         +-----------+   +--------+
           | ApplicationThread |                         |Instrumentation|           |LoadApk|         |ContextImpl|   |Activity|
           +------------+------+                         +-------+-------+           +---+---+         +-----+-----+   +----+---+
                        |                                        |                       |                   |              |
                        |                                        |                       |                   |              |
                        |                                        |                       |                   |              |
 scheduleLaunchActivity |                                        |                       |                   |              |
----------------------> |  handlerMessage                        |                       |                   |              |
                        +-------------+                          |                       |                   |              |
                        |             |                          |                       |                   |              |
                        | <-----------+                          |                       |                   |              |
                        |  LAUNCH_ACTIVITY                       |                       |                   |              |
                        |                                        |                       |                   |              |
                        |                                        |                       |                   |              |
                        |  handleLaunchActivity                  |                       |                   |              |
                        +----------------------+                 |                       |                   |              |
                        |                      |                 |                       |                   |              |
                        | <--------------------+                 |                       |                   |              |
                        |                                        |                       |                   |              |
                        |  performLaunchActivity                 |                       |                   |              |
                        +------------------------+               |                       |                   |              |
                        |                        |               |                       |                   |              |
                        | <----------------------+               |                       |                   |              |
                        |           getClassLoader               |                       |                   |              |
                        +--------------------------------------------------------------> |                   |              |
                        |                                        |                       |                   |              |
                        |            newActivity                 |                       |                   |              |
                        +--------------------------------------> |                       |                   |              |
                        |                                        |                       |                   |              |
                        |        makeApplication                 |                       |                   |              |
                        +--------------------------------------------------------------> |                   |              |
                        |                                        |                       |                   |              |
                        |       new & init                       |                       |                   |              |
                        +----------------------------------------------------------------------------------> |              |
                        |                                        |                       |                   |              |
                        |          attach(init activity)         |                       |                   |              |
                        +-------------------------------------------------------------------------------------------------> |
                        |                                        |                       |                   |              |
                        |     callActivityOnCreate               |                       |                   |              |
                        +--------------------------------------> |                       |                   |              |
                        |                                        |                       |                   |              |
                        |                                        |                       |                   |              |
                        |                                        |                       |                   |              |
                        |                                        |                       |                   |              |
                        +                                        +                       +                   +              +

来源

  1. 一个Android应用窗口的运行上下文环境是使用一个ContextImpl对象来描述的,这个ContextImpl对象会分别保存在Activity类的父类ContextThemeWrapper和ContextWrapper的成员变量mBase中,即ContextThemeWrapper类和ContextWrapper类的成员变量mBase指向的是一个ContextImpl对象。
  2. Activity组件在创建过程中,即在它的成员函数attach被调用的时候,会创建一个PhoneWindow对象,并且保存在成员变量mWindow中,用来描述一个具体的Android应用程序窗口。
  3. Activity组件在创建的最后,即在它的子类所重写的成员函数onCreate中,会调用父类Activity的成员函数setContentView来创建一个Android应用程序窗口的视图。

附整个Activity启动流程涉及到的代码片段:Gist

kupbezrecepty.com

发表评论

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据