Activity启动流程

####

Activity启动流程
  1. context.startActivity调用AMS系统服务,发起binder调用

  2. 是否需要创建进程,执行进程启动流程,同时挂起Activity或者service等相关组件,等待进程启动完成

    #进程启动流程
    2.1 AMS像Zygote通过socket发送命令启动进程
    2.2 Zygote收到命令会调用runeOnce方法,fork进程
    2.3 zygote fork用进程后,会执行ActivityThread的Main函,这个ActivityThread类入口来自AMS发送的Socket参数
    2.4 进程启动后会像AMS报告,attachApplication binder调用
    
  3. 进程启动好后AMS 通过binder调用应用的bindApplication,初始化应用

  4. 创建Acitivty 对象,准备好aplication,创建contextImpl,attach 上下文

  5. 生命周期回调 OnCreate

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    /**  Core implementation of activity launch. */
    private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    ContextImpl appContext = createBaseContextForActivity(r);

    try {
    java.lang.ClassLoader cl = appContext.getClassLoader();
    activity = mInstrumentation.newActivity(
    cl, component.getClassName(), r.intent);
    } catch (Exception e) {
    if (!mInstrumentation.onException(activity, e)) {
    throw new RuntimeException(
    "Unable to instantiate activity " + component
    + ": " + e.toString(), e);
    }
    }

    try {
    Application app = r.packageInfo.makeApplicationInner(false, mInstrumentation);
    if (activity != null) {
    activity.attach(appContext, this, getInstrumentation(), r.token,
    r.ident, app, r.intent, r.activityInfo, title, r.parent,
    r.embeddedID, r.lastNonConfigurationInstances, config,
    r.referrer, r.voiceInteractor, window, r.activityConfigCallback,
    r.assistToken, r.shareableActivityToken);
    if (theme != 0) {
    activity.setTheme(theme);
    }
    if (r.isPersistable()) {
    mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
    } else {
    mInstrumentation.callActivityOnCreate(activity, r.state);
    }
    }

    } catch (SuperNotCalledException e) {
    throw e;

    } catch (Exception e) {
    }

    return activity;
    }
  6. 接着activity 显示原理