源码学习->05Context

参考文章 :

1. 理解Android Context
理解Application创建过程

Context :

  • 1、ApplicationContext;
  • 2、ActivityContext;
  • 3、ServiceContext;

一、ApplicationContext:

1.1 ApplicationContext.handleMessage:
  • App启动过程中, 首先通过H.sendMessage(BIND_APPLICATION)触发app的Application被初始化;
ActivityThread.class:
public void handleMessage(Message msg) {
    switch (msg.what) {
        case BIND_APPLICATION:
            AppBindData data = (AppBindData)msg.obj;
            // 模块<1.2>
            handleBindApplication(data);
            break;
    }
}
1.2 ApplicationContext.handleBindApplication:
private void handleBindApplication(AppBindData data) {
    ...
    if (data.instrumentationName != null) {
         ...
    } else {
        mInstrumentation = new Instrumentation();
    }
    /**
     * 构建Application; 模块<1.3>
     */
    Application app = data.info.makeApplication(data.restrictedBackupMode, null);
    mInitialApplication = app;
    /**
     * 触发Application.onCreate执行;
     */
    mInstrumentation.callApplicationOnCreate(app);
}
1.3 LoadedApk.makeApplication:
public Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation) {
    ...
    Application app = null;

    String appClass = mApplicationInfo.className;
    if (forceDefaultAppClass || (appClass == null)) {
        appClass = "android.app.Application";
    }
    java.lang.ClassLoader cl = getClassLoader();
    ...
    /**
     * 1. 构建ContextImpl; 模块<1.4>
     * 2. 构建Application, 并传入ContextImpl对象; 对象<1.5>
     * 3. Context持有Application的引用;
     */
    ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
    app = mActivityThread.mInstrumentation.newApplication(cl, appClass, appContext);
    appContext.setOuterContext(app);
    ...
    if (instrumentation != null) {
        instrumentation.callApplicationOnCreate(app);
    }
    return app;
}
1.5 ContextImpl.createAppContext:
static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
     return new ContextImpl(null, mainThread,
                packageInfo, null, null, false, null, null, Display.INVALID_DISPLAY);
}
1.6 Instrumentation.newApplication:
Instrumentation.class:
public Application newApplication(ClassLoader cl, String className, Context context) {
    return newApplication(cl.loadClass(className), context);
}

static public Application newApplication(Class clazz, Context context) {
    Application app = (Application)clazz.newInstance();
    app.attach(context);
    return app;
}

Application.class:

Context mBase;

final void attach(Context context) {
    attachBaseContext(context);
    mLoadedApk = ContextImpl.getImpl(context).mPackageInfo;
}

protected void attachBaseContext(Context base) {
    mBase = base;
}
  • 关于Application与Context的关系:
源码学习->05Context_第1张图片
Application与Context的关系

二、ActivityContext :

2.1 ActivityThread.performLaunchActivity:
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    if (activity != null) {
        /**
         * 1. 与模块<1.5>类似, 构建ContextImpl对象;<模块2.2>
         * 2. activity通过attach持有ContextImpl的引用;
         */
        Context appContext = createBaseContextForActivity(r, activity);
        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);
    }
}
2.2 ActivityThread.createBaseContextForActivity:
private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) {
    ...
    /**
     * 1. 构建ContextImpl;
     * 2. ContextImpl持有Activity的引用;
     */
    ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, displayId, r.overrideConfig);
    appContext.setOuterContext(activity);
    return baseContext;
}
  • Activity与ContextImpl的关系:
源码学习->05Context_第2张图片
Activity与ContextImpl的关系

你可能感兴趣的:(源码学习->05Context)