Android global access to Context skills (using Context anytime, anywhere)

Reference: Chapter 13 of "The First Line of Code"

#####################################################

Many places need to use Context, pop up Toast / start activity / send broadcast / operate database / use notification, etc.

Activity itself is a Context object, but when the architecture of the application starts to be complicated, a lot of logic code will be separated from the Activity class, but at this time it is just necessary to use Context, then, the question is coming?

##################################################

The first method to get Context

Set a Context in the parameters of the called method, and pass in a Context when waiting for other method calls, then you can use

Although this is also a solution, but the task of obtaining the Context is transferred to the caller, without considering whether the caller can get the Context object, so this is not a good method

#####################################################

The second method (more effective)

Android provides an Application class, which is automatically initialized by the system whenever the application starts. And we can customize our own Application class in order to manage some global state information in the program, such as global Context

First, create a MyApplication class that inherits from Application:

package com.example.mylibrary;
 
import android.app.Application;
import android.content.Context;
 
/**
 * Created by root on 15-10-1.
 */
public class MyApplication extends Application {
    
    private static Context context;
 
    @Override
    public void onCreate() {
         super.onCreate(); 
         context = getApplicationContext();
    }
    
    public static Context getContext() {
        return context;
    }
}

In the above code, the onCreate () method of the parent class is rewritten, and an application-level Context is obtained by calling the getApplicationContext () method, and then a static getContext method is provided to return the Context just obtained


Next, we need to inform the system that the MyApplication class should be initialized when the program starts, rather than the default Application class.

Just specify it under the <application> tag in the AndroidManifest.xml file:

    <application
        android:name="com.example.mylibrary.MyApplication"

Note: When specifying MyApplication here, you must add the complete package name, otherwise the system will not be able to find this class


In this way, a mechanism for obtaining the Context globally is realized, and no matter you want to use the Context anywhere in the project, you only need to call MyApplication.getContext ()

Toast.makeText(MyApplication.getContext(), "hello mylibrary", Toast.LENGTH_SHORT).show();


————————————————
Copyright Statement: This article is an original article by CSDN blogger "No. 1993", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement.
Original link: https://blog.csdn.net/u012005313/article/details/48848045

Published 31 original articles · Likes6 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/u012824529/article/details/103673010