The process of creating Context in Acitivy (2)

page4
在这里我们分析一下ContextImpl的init函数的具体实现:
final void init(LoadedApk packageInfo, IBinder activityToken, ActivityThread mainThread) {
        init(packageInfo, activityToken, mainThread, null, null, Process.myUserHandle());
    }

1     final void init(LoadedApk packageInfo, IBinder activityToken, ActivityThread mainThread,
2             Resources container, String basePackageName, UserHandle user) {
3         mPackageInfo = packageInfo;
4         mBasePackageName = basePackageName != null ? basePackageName : packageInfo.mPackageName;
5         mResources = mPackageInfo.getResources(mainThread);
6
7         if (mResources != null && container != null
8                 && container.getCompatibilityInfo().applicationScale !=
9                         mResources.getCompatibilityInfo().applicationScale) {
10             if (DEBUG) {
11                 Log.d(TAG, "loaded context has different scaling. Using container's" +
12                         " compatiblity info:" + container.getDisplayMetrics());
13             }
14             mResources = mainThread.getTopLevelResources(
15                     mPackageInfo.getResDir(), Display.DEFAULT_DISPLAY,
16                     null, container.getCompatibilityInfo());
17         }
18         mMainThread = mainThread;
19         mActivityToken = activityToken;
20 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
21 mUser = user;
22 }

The main logic of the init function is to initialize the main member variable
page5
activity The definition of the attach function is as follows, note the following are two functions:
1 final void attach(Context context, ActivityThread aThread, Instrumentation instr, IBinder token,
2 Application application, Intent intent, ActivityInfo info, CharSequence title,
3 Activity parent, String id, NonConfigurationInstances lastNonConfigurationInstances,
4 Configuration config) {
5 attach(context, aThread, instr , token, 0, application, intent, info, title, parent, id,
6 lastNonConfigurationInstances, config);
7     }
8    
9     final void attach(Context context, ActivityThread aThread,
10             Instrumentation instr, IBinder token, int ident,
11             Application application, Intent intent, ActivityInfo info,
12             CharSequence title, Activity parent, String id,
13             NonConfigurationInstances lastNonConfigurationInstances,
14             Configuration config) {
15         attachBaseContext(context);
16
17         mFragments.attachActivity(this, mContainer, null);
18        
19         mWindow = PolicyManager.makeNewWindow(this);
20         mWindow.setCallback(this);
21         mWindow.getLayoutInflater().setPrivateFactory(this);
22         if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
23             mWindow.setSoftInputMode(info.softInputMode);
24         }
25         if (info.uiOptions != 0) {
26             mWindow.setUiOptions(info.uiOptions);
27         }
28         mUiThread = Thread.currentThread();
29        
30         mMainThread = aThread;
31         mInstrumentation = instr;
32         mToken = token;
33         mIdent = ident;
34         mApplication = application;
35         mIntent = intent;
36         mComponent = intent.getComponent();
37         mActivityInfo = info;
38         mTitle = title;
39         mParent = parent;
40         mEmbeddedID = id;
41         mLastNonConfigurationInstances = lastNonConfigurationInstances;
42
43         mWindow.setWindowManager(
44                 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
45                 mToken, mComponent.flattenToString(),
46                 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
47         if (mParent != null) {
48             mWindow.setContainer(mParent.getWindow());
49         }
50 mWindowManager = mWindow.getWindowManager();
51 mCurrentConfig = config;
52 }

Line 15 (Activity->attach) calls the attachBaseContext function. For a detailed analysis of the attachBaseContext function, please refer to the page6 file.
After calling the attachBaseContext function, we understand that the Activity takes With ContextImpl, and ContextImpl also holds this Activity
page6
In this article, we analyze the specific logic of the Activity's attachBaseContext function. In fact, the attachBaseContext function is inherited from the Activity's parent class, ContextThemeWrapper.
Let's take a look at the ContextThemeWrapper class Inheritance system,
public class ContextThemeWrapper extends ContextWrapper
public class ContextWrapper extends Context

Then the specific implementation of the attachBaseContext function of ContextThemeWrapper is as follows:
1 @Override protected void attachBaseContext(Context newBase) {
2 super.attachBaseContext(newBase);
3 mBase = newBase;
4 }

Line 2 (ContextThemeWrapper->attachBaseContext) calls the attachBaseContext function of the parent class ContextWrapper:
1 protected void attachBaseContext(Context base) {
2 if (mBase != null) {
3 throw new IllegalStateException("Base context already set");
4 }
5 mBase = base;
6 }
The attachBaseContext function of ContextWrapper will set the member variable mBase to point to a Context object, which is ContextImpl.
The member variable mBase of ContextWrapper The definition is as follows:
Context mBase;

the third line (ContextThemeWrapper->attachBaseContext) will initialize the member variable mBase, I rely on! Why have another!
The definition of mBase of ContextThemeWrapper is as follows:
private Context mBase;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326165579&siteId=291194637