The difference and usage of getApplication() and getApplicationContext in Android

getApplication()

        To understand the getApplication() method, you must first understand the Application class. The Android system automatically creates an object of the Application class for each program runtime and only one is created, so Application can be said to be a class in the singleton mode.

        Application, like Activity and Service, is a system component of the Android framework. When the Android program starts, the system creates an Application object to store some information about the system.

        From the source code, we can find that both Application and Activity inherit from Context. They are all environment contexts, but Application is the environment that exists when our application (or package) starts, and Activity is an interface environment. Application is initialized as soon as the application is created, and it always exists when the application is running. Then we can use it as a global variable, which can save some shared data, or do some initialization work of tool classes. In normal development, sometimes some global data may be needed. Usually, a new class is created to inherit Application, and getApplication() is used to obtain an object instance of Application:

public class MyApplication extends Application {}

You can do some initialization by overriding the onCreate() method:

 @Override
 public void onCreate() {
      super.onCreate();
       ToastUtils.register(this);
       //LeakCanary检测OOM
       LeakCanary.install(this);
 }

Finally, you need to make a statement in the Manifest.xml file:

<application
    android:name=".MyApplication"
    ...
</application>

When obtaining the Application, if it is in the case of Context, it can be obtained directly through (MyApplication)getApplication(). There is also a way to achieve acquisition by imitating the practice of singleton without Context:

public class MyApplication extends Application {
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }
     
    // 获取Application
    public static Context getMyApplication() {
        return instance;
    }
}

getApplicationContext()

        getApplicationContext() returns the context of the application, that is, the Application is used as the Context. The life cycle is the entire application, and the application destroys it until it is destroyed. Here to distinguish the Context of the Activity, the context of Activity.this returns the context of the current Activity, and uses the Activity as the Context, the life cycle belongs to the Activity, and the Activity destroys it.

        Attention should be paid to the use: methods related to UI are basically not recommended or cannot be used in Application. As long as you grasp one point, you should use Activity as Context to handle everything related to UI.
———————————————

Reprinted in: https://blog.csdn.net/u014665856/article/details/72354406

Guess you like

Origin blog.csdn.net/weixin_42602900/article/details/123084914