Page1
Starting from this article, let's analyze the process of Activity creating Context.
Context is created in the performLaunchActivity function of ActivityThread, so we start the analysis from the performLaunchActivity function as the entry:
1 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
2 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
3
4 ActivityInfo aInfo = r.activityInfo;
5 if (r.packageInfo == null) {
6 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
7 Context.CONTEXT_INCLUDE_CODE);
8 }
9
10 ComponentName component = r.intent.getComponent();
11 if (component == null) {
12 component = r.intent.resolveActivity(
13 mInitialApplication.getPackageManager());
14 r.intent.setComponent(component);
15 }
16
17 if (r.activityInfo.targetActivity != null) {
18 component = new ComponentName(r.activityInfo.packageName,
19 r.activityInfo.targetActivity);
20 }
21
22 Activity activity = null;
23 try {
24 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
25 activity = mInstrumentation.newActivity(
26 cl, component.getClassName(), r.intent);
27 StrictMode.incrementExpectedActivityCount(activity.getClass());
28 r.intent.setExtrasClassLoader(cl);
29 if (r.state != null) {
30 r.state.setClassLoader(cl);
31 }
32 } catch (Exception e) {
33 if (!mInstrumentation.onException(activity, e)) {
34 throw new RuntimeException(
35 "Unable to instantiate activity " + component
36 + ": " + e.toString(), e);
37 }
38 }
39
40 try {
41 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
42
43 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
44 if (localLOGV) Slog.v(
45 TAG, r + ": app=" + app
46 + ", appName=" + app.getPackageName()
47 + ", pkg=" + r.packageInfo.getPackageName()
48 + ", comp=" + r.intent.getComponent().toShortString()
49 + ", dir=" + r.packageInfo.getAppDir());
50
51 if (activity != null) {
52 Context appContext = createBaseContextForActivity(r, activity);
53 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
54 Configuration config = new Configuration(mCompatConfiguration);
55 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
56 + r.activityInfo.name + " with config " + config);
57 activity.attach(appContext, this, getInstrumentation(), r.token,
58 r.ident, app, r.intent, r.activityInfo, title, r.parent,
59 r.embeddedID, r.lastNonConfigurationInstances, config);
60
61 if (customIntent != null) {
62 activity.mIntent = customIntent;
63 }
64 r.lastNonConfigurationInstances = null;
65 activity.mStartedActivity = false;
66 int theme = r.activityInfo.getThemeResource();
67 if (theme != 0) {
68 activity.setTheme(theme);
69 }
70
71 activity.mCalled = false;
72 mInstrumentation.callActivityOnCreate(activity, r.state);
73 if (!activity.mCalled) {
74 throw new SuperNotCalledException(
75 "Activity " + r.intent.getComponent().toShortString() +
76 " did not call through to super.onCreate()");
77 }
78 r.activity = activity;
79 r.stopped = true;
80 if (!r.activity.mFinished) {
81 activity.performStart();
82 r.stopped = false;
83 }
84 if (!r.activity.mFinished) {
85 if (r.state != null) {
86 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
87 }
88 }
89 if (!r.activity.mFinished) {
90 activity.mCalled = false;
91 mInstrumentation.callActivityOnPostCreate(activity, r.state);
92 if (!activity.mCalled) {
93 throw new SuperNotCalledException(
94 "Activity " + r.intent.getComponent().toShortString() +
95 " did not call through to super.onPostCreate()");
96 }
97 }
98 }
99 r.paused = true;
100
101 mActivities.put(r.token, r);
102
103 } catch (SuperNotCalledException e) {
104 throw e;
105
106 } catch (Exception e) {
107 if (!mInstrumentation.onException(activity, e)) {
108 throw new RuntimeException(
109 "Unable to start activity " + component
110 + ": " + e.toString(), e );
111 }
112 }
113
114 return activity;
115 }
Line 52 (ActivityThread->performLaunchActivity) calls the createBaseContextForActivity function to create a Context object for the activity object just created. For a detailed analysis of the performLaunchActivity function, please refer to the page2 file.
pp. 57-59 The line (ActivityThread->performLaunchActivity) calls the attach function of the Activity. For a detailed analysis of the attach function, please refer to the page5 file.
page2
ActivityThread的createBaseContextForActivity函数定义如下:
1 private Context createBaseContextForActivity(ActivityClientRecord r,
2 final Activity activity) {
3 ContextImpl appContext = new ContextImpl();
4 appContext.init(r.packageInfo, r.token, this);
5 appContext.setOuterContext(activity);
6
7 // For debugging purposes, if the activity's package name contains the value of
8 // the "debug.use-second-display" system property as a substring, then show
9 // its content on a secondary display if there is one.
10 Context baseContext = appContext;
11 String pkgName = SystemProperties.get("debug.second-display.pkg");
12 if (pkgName != null && !pkgName.isEmpty()
13 && r.packageInfo.mPackageName.contains(pkgName)) {
14 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
15 for (int displayId : dm.getDisplayIds()) {
16 if (displayId != Display.DEFAULT_DISPLAY) {
17 Display display = dm.getRealDisplay(displayId);
18 baseContext = appContext.createDisplayContext(display);
19 break;
20 }
21 }
22 }
23 return baseContext;
24 }
Line 3 (ActivityThread->createBaseContextForActivity) will create a new ContextImpl object. For a detailed analysis of the constructor of ContextImpl, please refer to the page3 file.
Line 4 (ActivityThread->createBaseContextForActivity) will call the init function of ContextImpl, for details on the init function The analysis can refer to the page4 file.
Line 5 (ActivityThread->createBaseContextForActivity) calls the setOuterContext function of ContextImpl. The setOuterContext function of ContextImpl is defined as follows:
final void setOuterContext(Context context) {
mOuterContext = context;
} In
this way, ContextImpl will also hold the activity object . What does line 10-22 (ActivityThread-> createBaseContextForActivity
) do? I don't know. On page 3 , we analyze the construction process of the ContextImpl class. Let's first look at the inheritance system of the ContextImpl class. The definition of the ContextImpl class is as follows: class ContextImpl extends Context { public abstract class Context {
The constructor of the ContextImpl class is as follows:
ContextImpl() {
mOuterContext = this;
}
In the constructor of ContextImpl, only the member variable mOuterContext is initialized to point to the ContextImpl object. The definition of the member variable mOuterContext
is as follows:
private Context mOuterContext;
Yes, it's that simple.
The process of creating Context in Acitivy (1)
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326165265&siteId=291194637
Recommended
Ranking