startActivity启动过程分析和Activity生命周期

一、startActivity启动过程

startActivity启动过程分析和Activity生命周期_第1张图片
启动流程:

  1. 点击桌面App图标,Launcher进程采用Binder IPC向system_server进程发起startActivity请求;
  2. system_server进程接收到请求后,向zygote进程发送创建进程的请求;
  3. Zygote进程fork出新的子进程,即App进程;
  4. App进程,通过Binder IPC向sytem_server进程发起attachApplication请求;
  5. system_server进程在收到请求后,进行一系列准备工作后,再通过binder IPC向App进程发送6. scheduleLaunchActivity请求;
  6. App进程的binder线程(ApplicationThread)在收到请求后,通过handler向主线程发送LAUNCH_ACTIVITY消息;
  7. 主线程在收到Message后,通过发射机制创建目标Activity,并回调Activity.onCreate()等方法。

官方的Activity状态转换图:
startActivity启动过程分析和Activity生命周期_第2张图片
Activity的生命周期中只有在以下3种状态之一,才能较长时间内保持状态不变。

  • Resumed(运行状态):Activity处于前台,且用户可以与其交互。
  • Paused(暂停状态): Activity被在前台中处于半透明状态或者未覆盖全屏的其他Activity部分遮挡。 暂停的Activity不会接收用户输入,也无法执行任何代码。
  • Stopped(停止状态):Activity被完全隐藏,且对用户不可见;被视为后台Activity。 停止的Activity实例及其诸如成员变量等所有状态信息将保留,但它无法执行任何代码。

除此之外,其他状态都是过渡状态(或称为暂时状态),比如onCreate(),onStart()后很快就会调用onResume()方法。

二、生命周期

2.1 进程间通信

对于App来说,其Activity的生命周期执行是与系统进程中的ActivityManagerService有一定关系的,接下来从进程和线程的角度来分析Activity的生命周期,这里涉及到系统进程和应用进程:

system_server进程是系统进程,Java framework框架的核心载体,里面运行了大量的系统服务,比如这里提供ApplicationThreadProxy(简称ATP),ActivityManagerService(简称AMS),这个两个服务都运行在system_server进程的不同线程中,由于ATP和AMS都是基于IBinder接口,都是binder线程,binder线程的创建与销毁都是由binder驱动来决定的。

App进程是应用程序所在进程,主线程主要负责Activity/Service等组件的生命周期以及UI相关操作都运行在这个线程; 另外,每个App进程中至少会有两个binder线程 ApplicationThread(简称AT)和ActivityManagerProxy(简称AMP),除了下图中所示的线程,其实还有很多线程,比如signal catcher线程等。
startActivity启动过程分析和Activity生命周期_第3张图片
Binder用于不同进程之间通信,由一个进程的Binder客户端向另一个进程的服务端发送事件,比如图中线程2向线程4发送事务;而handler用于同一个进程中不同线程的通信,比如图中线程4向主线程发送消息。

结合图说说Activity生命周期,比如暂停Activity的流程如下:

  • 线程1的AMS中调用线程2的ATP来发送事件;(由于同一个进程的线程间资源共享,可以相互直接调用,但需要注意多线程并发问题)
  • 线程2通过binder将暂停Activity的事件传输到App进程的线程4;
  • 线程4通过handler消息机制,将暂停Activity的消息发送给主线程;
  • 主线程在looper.loop()中循环遍历消息,当收到暂停Activity的消息(PAUSE_ACTIVITY)时,便将消息分发给ActivityThread.H.handleMessage()方法,再经过方法的层层调用,最后便会调用到Activity.onPause()方法。

这便是由AMS完成了onPause()控制,同理Activity的其他生命周期也是这么个流程来进行控制的。

2.2 App主线程

每个App都有一个主线程,大家常说主线程是ActivityThread,其实这个说法是欠妥当的,首先何为线程?一般来说Java层创建线程往往是通过继承Thread对象或者实现Runnable,再看看ActivityThread,会发现该对象并没有继承任何对象。准确说法ActivityThread是运行在主线程的对象,充当着主线程的职责。

那主线程到底是哪个呢,这个问题涉及到Linux进程与线程的理解,本质上来说大家常说的主线程就是app首次启动时创建的进程,对于Linux来说进程与线程都是一个task_struct结构体,除了是否有独立资源,并没有什么区别。

那有为何说充当着主线程的职责呢?这是由于进程在创建之初ActivityThread会为主线程创建Looper对象,这个便是用来维护Activity的生命周期。

2.3 枢纽中心

Activity的生命周期,都是其他线程通过handler发送消息给主线程,那么主线程中的ActivityThread的内部类H控制整个核心消息处理机制,通过H.handleMessage()来控制Activity的生命周期,在H类中共定义了50种消息。

private class H extends Handler {
  public static final int LAUNCH_ACTIVITY         = 100;
  public static final int PAUSE_ACTIVITY          = 101;
  public static final int PAUSE_ACTIVITY_FINISHING= 102;
  public static final int STOP_ACTIVITY_SHOW      = 103;
  public static final int STOP_ACTIVITY_HIDE      = 104;
  public static final int SHOW_WINDOW             = 105;
  public static final int HIDE_WINDOW             = 106;
  public static final int RESUME_ACTIVITY         = 107;
  public static final int SEND_RESULT             = 108;
  public static final int DESTROY_ACTIVITY        = 109;
  public static final int BIND_APPLICATION        = 110;
  public static final int EXIT_APPLICATION        = 111;
  public static final int NEW_INTENT              = 112;
  public static final int RECEIVER                = 113;
  public static final int CREATE_SERVICE          = 114;
  public static final int SERVICE_ARGS            = 115;
  public static final int STOP_SERVICE            = 116;

  public static final int CONFIGURATION_CHANGED   = 118;
  public static final int CLEAN_UP_CONTEXT        = 119;
  public static final int GC_WHEN_IDLE            = 120;
  public static final int BIND_SERVICE            = 121;
  public static final int UNBIND_SERVICE          = 122;
  public static final int DUMP_SERVICE            = 123;
  public static final int LOW_MEMORY              = 124;
  public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
  public static final int RELAUNCH_ACTIVITY       = 126;
  public static final int PROFILER_CONTROL        = 127;
  public static final int CREATE_BACKUP_AGENT     = 128;
  public static final int DESTROY_BACKUP_AGENT    = 129;
  public static final int SUICIDE                 = 130;
  public static final int REMOVE_PROVIDER         = 131;
  public static final int ENABLE_JIT              = 132;
  public static final int DISPATCH_PACKAGE_BROADCAST = 133;
  public static final int SCHEDULE_CRASH          = 134;
  public static final int DUMP_HEAP               = 135;
  public static final int DUMP_ACTIVITY           = 136;
  public static final int SLEEPING                = 137;
  public static final int SET_CORE_SETTINGS       = 138;
  public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
  public static final int TRIM_MEMORY             = 140;
  public static final int DUMP_PROVIDER           = 141;
  public static final int UNSTABLE_PROVIDER_DIED  = 142;
  public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
  public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
  public static final int INSTALL_PROVIDER        = 145;
  public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
  public static final int CANCEL_VISIBLE_BEHIND = 147;
  public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
  public static final int ENTER_ANIMATION_COMPLETE = 149;
}

主线程每到收到其他线程发送过来的不同的Handler消息,则都会触发相应的H.handleMessage,下面是与Activity相关的一些常见消息。

  • LAUNCH_ACTIVITY
  • RELAUNCH_ACTIVITY
  • RESUME_ACTIVITY
  • NEW_INTENT
  • PAUSE_ACTIVITY / PAUSE_ACTIVITY_FINISHING
  • STOP_ACTIVITY_SHOW / STOP_ACTIVITY_HIDE
  • DESTROY_ACTIVITY

一般来说收到消息,都会调用相应handlexxx方法。比如,LAUNCH_ACTIVITY则对应handleLaunchActivityRESUME_ACTIVITY则对应handleResumeActivity等。

三、调用链

3.1 启动应用

消息: LAUNCH_ACTIVITY

调用链

ActivityThread.handleLaunchActivity
    ActivityThread.handleConfigurationChanged
        ActivityThread.performConfigurationChanged
            ComponentCallbacks2.onConfigurationChanged

    ActivityThread.performLaunchActivity
        LoadedApk.makeApplication
            Instrumentation.callApplicationOnCreate
                Application.onCreate

        Instrumentation.callActivityOnCreate
            Activity.performCreate
                Activity.onCreate

        Instrumentation.callActivityonRestoreInstanceState
            Activity.performRestoreInstanceState
                Activity.onRestoreInstanceState

    ActivityThread.handleResumeActivity
        ActivityThread.performResumeActivity
            Activity.performResume
                Activity.performRestart
                    Instrumentation.callActivityOnRestart
                        Activity.onRestart

                    Activity.performStart
                        Instrumentation.callActivityOnStart
                            Activity.onStart

                Instrumentation.callActivityOnResume
                    Activity.onResume

App角度

调用链过程层层调用,但对上层应用是透明的,App开发者只需要覆写其中重要的回调函数即可,故此处所说的App角度,便是指App开发者来说可见之处。经过上述的调用链,依次会执行下面回调方法。

  1. ComponentCallbacks2.onConfigurationChanged():
  2. Application.onCreate()
  3. Activity.onCreate()
  4. Activity.onRestoreInstanceState()
  5. Activity.onRestart()
  6. Activity.onStart()
  7. Activity.onResume()

Application和Activity都实现了ComponentCallbacks2接口;所以Application和Activity会先执行onConfigurationChanged()回调方法。在前面说过onCreate()是过渡状态,紧跟着会执行handleResumeActivity()方法,然后就进入Resumed状态。

3.2 恢复应用

消息: RESUME_ACTIVITY
调用链

ActivityThread.handleResumeActivity
    ActivityThread.performResumeActivity
        Activity.performResume
            Activity.performRestart
                Instrumentation.callActivityOnRestart
                    Activity.onRestart

                Activity.performStart
                    Instrumentation.callActivityOnStart
                        Activity.onStart

            Instrumentation.callActivityOnResume
                Activity.onResume

App角度

  1. Activity.onRestart()
  2. Activity.onStart()
  3. Activity.onResume()

App处于运行状态,UI可见。

3.3 暂停应用

消息: PAUSE_ACTIVITY
调用链

ActivityThread.handlePauseActivity
    ActivityThread.performPauseActivity
        ActivityThread.callCallActivityOnSaveInstanceState
            Instrumentation.callActivityOnSaveInstanceState
                Activity.performSaveInstanceState
                    Activity.onSaveInstanceState

        Instrumentation.callActivityOnPause
            Activity.performPause
                Activity.onPause

App角度

  1. Activity.onSaveInstanceState()
  2. Activity.onPause()

根据saveState是否true决定是否执行callCallActivityOnSaveInstanceState()分支,从而决定是否回调onRestoreInstanceState()方法

3.4 停止应用

消息: STOP_ACTIVITY_HIDE
调用链

ActivityThread.handleStopActivity
    ActivityThread.performStopActivityInner
        ActivityThread.callCallActivityOnSaveInstanceState
            Instrumentation.callActivityOnSaveInstanceState
                Activity.performSaveInstanceState
                    Activity.onSaveInstanceState

        ActivityThread.performStop
            Activity.performStop
                Instrumentation.callActivityOnStop
                    Activity.onStop

    updateVisibility

    H.post(StopInfo)
        AMP.activityStopped
            AMS.activityStopped
                ActivityStack.activityStoppedLocked
                AMS.trimApplications
                    ProcessRecord.kill
                    ApplicationThread.scheduleExit
                        Looper.myLooper().quit()

                    AMS.cleanUpApplicationRecordLocked
                    AMS.updateOomAdjLocked

App角度

  1. Activity.onSaveInstanceState
  2. Activity.onStop。

在停止Activity的过程,会有一个trimApplications()的操作,主要是kill空进程,将当前进程退出loop循环,清理应用的上下文环境,并且更新进程的Adj值。

3.5 销毁应用

消息: DESTROY_ACTIVITY

调用链

ActivityThread.handleDestroyActivity
    ActivityThread.performDestroyActivity
        Instrumentation.callActivityOnPause
        Activity.performStop()
        Instrumentation.callActivityOnDestroy
            Activity.performDestroy
                Window.destroy
                Activity.onDestroy

    AMP.activityDestroyed
        AMS.activityDestroyed
            ActivityStack.activityDestroyedLocked
                ActivityStackSupervisor.resumeTopActivitiesLocked
                    ActivityStack.resumeTopActivityLocked
                        ActivityStack.resumeTopActivityInnerLocked

App角度

  • Activity.onDestroy

销毁应用后,会查看第一个没有结束的Activity,用于显示在最顶层界面,当不存在未结束的Activity时,则显示Launcher界面,即主界面。

你可能感兴趣的:(startActivity启动过程分析和Activity生命周期)