In app development, we need to use app resources, such as text, picture, Activity, Service or broadcastReceiver, etc. GetApplicationContext () is also often used to get a Context object. So what is this Context?

Let’s take a look at the Context in Android. The main content of this paper is shown below.

ContextClass introduction

Context means context, context, background, context, etc. Context is a core function class that keeps the components of an Android application working properly.

Context is an abstract class. It is an “interface” to the app global environment and is provided by the Android system with inherited classes (such as Activity, Service, Application, etc.). It can connect to application resources and use application-level actions, such as launching activities, broadcasting and receiving intents.

The total number of contexts in the Application is: Total number of contexts = Number of Activities + Number of Services + 1

ContextA subclass of

Simple inheritance relationship

├─ Custom Exercises ─ Custom exercises ─ custom exercises ─ custom exercises ─ custom exercises ─ custom exercises ─ custom exercises ─ custom exercisesCopy the code

As you can see from the inheritance diagram, the Application class, Service class, and Activity class all inherit from the Context class. After the Application is started, a global Application Context object is created for the Application. The ContextImpl class is the actual implementation of Context. The ContextWrapper class is the wrapper class. You can add custom operations to ContextImpl without changing it. MBase in ContextWrapper is actually a ContextImpl object. The mOuterContext in the ContextImpl class is a Context object that points to the corresponding Activity or Service or Application.

ContextImpl

Context is an abstract class, and the subclass ContextImpl implements the methods of Context; Provides basic context objects for activities and other application components. ContextImpl.java (frameworks\base\core\java\android\app)

/** * Common implementation of Context API, which provides the base * context object for Activity and other application components. */
class ContextImpl extends Context { / *... * / }
Copy the code

ContextWrapper

The Wrapper is a Wrapper. ContextWrapper is the wrapper class for Context. The decorator pattern is used here, and a Context instance is passed in the constructor. ContextWrapper holds the ContextImpl object. Some operations can be added without changing ContextImpl.

ContextWrapper.java (frameworks\base\core\java\android\content)

/** * Proxying implementation of Context that simply delegates all of its calls to * another Context. Can be subclassed to modify behavior without changing * the original Context. */
public class ContextWrapper extends Context {
    Context mBase;

    public ContextWrapper(Context base) {
        mBase = base;
    }
    // ...
}
Copy the code

The Application, Activity, and Service classes intersect with ContextImpl.

ContextThemeWrapper

Allows you to modify a theme in an enclosed context

ContextThemeWrapper.java (frameworks\base\core\java\android\view)

/** * A ContextWrapper that allows you to modify the theme from what is in the * wrapped context. */
public class ContextThemeWrapper extends ContextWrapper { / *... * / }
Copy the code

This provides a method for theme, which is related to android: Theme in app development. The same code, the same call, using a different theme will have different effects.

GetApplicationContext () and getBaseContext ()

public class ContextWrapper extends Context { Context mBase; .@Override
    public Context getApplicationContext(a) {
        returnmBase.getApplicationContext(); }.../ * * *@return the base context as set by the constructor or setBaseContext
     */
    public Context getBaseContext(a) {
        return mBase;// Don't use getBaseContext(), just use the Context you have.}... }Copy the code
getApplicationContext() = android.app.Application@39d42b0e
getBaseContext() = android.app.ContextImpl@1f48c92f
Copy the code

GetApplicationContext () gets the context from Application. GetBaseContext () is derived from the implementation class ContextImpl.

Context Subclass creation process

Application creation process

Let’s focus on the application and ignore the rest of the source code for now.

Process Description:

LoadedApk obtains the application class through the loadClass(className) of the classLoader, and then creates an application instance through clazz.newinstance (). The app.attach(context) method is then called to complete the initialization. The Attach method of the Application calls attachBaseContext(context) and assigns the ContextImpl instance created in the first step to the mBase member variable of ContextWrapper. At this point the Application instance creation is complete.

Activity creation – performLaunchActivity

The focus here is on the instantiation of the Activity and the preparatory process that precedes it.

Process analysis:

Focus on ActivityThread’s performLaunchActivity method. Get a ContextImpl instance, called appContext, from ContextImpl createActivityContext. The newActivity of Instrumentation returns an Activity instance. LoadedApk makeApplication can get the current application, if not currently is to create a new application. . At last, by ContextImpl setOuterContext and the Activity of the attach method, will ContextImpl instance associated with the Activity instance together.

use

The custom Application

Create a new MyApplication class that inherits from Application

import android.app.Application;
import android.content.Context;

public class MyApplication extends Application {
    private static Context context;

    public static Context getMyContext(a) {
        return context;
    }

    @Override
    public void onCreate(a) {
        super.onCreate(); context = getApplicationContext(); }}Copy the code

Use MyApplication in the Manifest;

    <application
        android:name="com.rust.aboutview.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">.Copy the code

You can call the getMyContext() method in different places

MyApplication.getMyContext()
Copy the code

reference

Blog.csdn.net/yanbober/ar…

Xujiaojie. Making. IO / 2017/09/18 /…

Xujiaojie. Making. IO / 2017/09/18 /…

Juejin. Cn/post / 684490…