What is the

From the perspective of application scenario, it is a scenario, a process of user interaction with the system. For example, when you look at a text message, the scene includes the page of the text message, and the data hidden behind it

When it comes to pages, we should be able to think of activities

Yes,Activity,Service is a Context

From the Perspective of the JAVA language,Context is an abstract class that contains some functions of the Application environment. From the perspective of design,Context provides only some functions. Extends is the essence of a class. An Activity is essentially a Context, and the other interfaces it implements simply extend the functionality of the Context. The extended class is called an Activity or Service

How many the Context

  • Application a Context
  • There are as many contexts as there are activities
  • There are as many contexts as there are services

Context =1 + Activities + Services

Application Context creation

As you can see from the analysis in the blog activityThread. main procedure, makeApplication is called in the handleBindApplication function

MakeApplication creates the Application and ContextImpl

Create ContextImpl

ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
Copy the code

Here this is the LoadedApk object, which is assigned in the handleBinderApplication

data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Copy the code

In this function, AppBindData(the argument in the handleBinderApplication) The mPackageName of ApplicationInfo creates a PackageInfo object and saves it as a global object of the ActivityThread class

Obviously, all activities or Applications or servies in an Application will have the same mPackageName, which is the package name, so the ActivityThread will only have a global PackageInfo object

Application Attach is called in the newApplication function

attach

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

Check the attachBaseContext

protected void attachBaseContext(Context base) {
        if(mBase ! =null) {
            throw new IllegalStateException("Base context already set");
        }
        mBase = base;
    }
Copy the code

So this mBase is the Context in ContextWrapper

The Activity Context is created

In the Launcher to start the process of analysis, can know handleLaunchActivity will call to performLaunchActivity, this function will be called createBaseContextForActivity

    private ContextImpl createBaseContextForActivity(ActivityClientRecord r) {
        final intdisplayId; . ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, r.activityInfo, r.token, displayId, r.overrideConfig); .return appContext;
    }
Copy the code

createActivityContext

The packageInfo in createActivityContext is basically the same as the flow analyzed in the previous section, and it is also global

static ContextImpl createActivityContext(ActivityThread mainThread,
            LoadedApk packageInfo, ActivityInfo activityInfo, IBinder activityToken, int displayId,
            Configuration overrideConfiguration) {... ContextImpl context =new ContextImpl(null, mainThread, packageInfo, activityInfo.splitName,
                activityToken, null.0, classLoader); .return context;
    }
Copy the code

attach

After creating the Context, call the attach function of the 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, window, r.configCallback);
Copy the code

The attach function does a lot of assignment. AttachBaseContext is the same as Application attachBaseContext, assigning the context to ContextWrapper’s mBase

Therefore, when we browse the Activity source code and see mBase, we should look for the ContextImpl method :::

The Service Context is created

A Service starts like an Activity, and eventually invokes a scheduleCreateService function, followed by a handleCreateService

The Context is created in the handleCreateService

ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
context.setOuterContext(service);
Application app = packageInfo.makeApplication(false, mInstrumentation);
service.attach(context, this, data.info.name, data.token, app,
                    ActivityManager.getService());
service.onCreate();
Copy the code

Context is created in the same way as Application. Attach is called after the Context is created to perform some assignment, as well as the mBase analyzed earlier

conclusion

Source of the PackageInfo object in different Context subclasses

The name of the class Remote data class Local data class Assignment way
Application ApplicationInfo AppBindData getPackageInfoNoCheck
Activity ActivityInfo ActivityClientRecord getPackageInfo
Service ServiceInfo CreateServiceData getPackageInfoNoCheck

Reference book: Android kernel anatomy