Small knowledge, big challenge! This article is part of the “Programmer’s Essentials

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

When I was learning Android, I always had a headache about context, because I wanted to make some tool classes, but it was not one of the four components, so I always had to pass context back and forth. I felt it was very troublesome, so I wanted to get the context globally. Finally, I saw that application was a global singleton, so I changed it.

Context; Context; Background; Context is often referred to as “context” in development, so what does this “context” mean? In language, we can understand it as context; in program, we can understand it as an environment where the current object is in the program, a process of interaction with the system. For example, in wechat chat, the “environment” refers to the chat interface and related data requests and transmission. Context is involved in loading resources, starting activities, obtaining system services, creating views and other operations.

So what is Context? An Activity is a Context, and a Service is a Context. Android programmers abstract the “scene” into a Context class, thinking that every interaction between a user and the operating system is a scene, such as a phone call or a text message, which is a scene with an interface, and some scenes without an interface, such as a Service running in the background. An application can be thought of as a work environment that users in this environment will switch to a different scene, it’s like a secretary at the front desk, she may need to receive guests, may have to print documents, may also to answering customer calls, the call it different scenarios, the front desk secretary could be called an application.

The idea is simple, the implementation is not complicated, the main idea is to do a singleton, save the context

  • 1. We need to rewrite the onCreate method in the Application, which initializes a Map so that we can insert arbitrary values. This variable can then be manipulated in various files throughout the application.
  • 2. There are two ways to get app in the program: getInstance() and getApplication() in the four components
package com.xin.app;

import java.util.HashMap;
import java.util.Map;

import android.app.Application;
import android.util.Log;
/ * * * *@author Silence skunk *@url http://write.blog.csdn.net/mdeditor#! postId=46674445 *@tip Welcome to reprint * */
public class App extends Application {

    private static App instance;
    private Map innerMap = new HashMap();

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;// Note that this is not the usual singleton approach
    }

    public static App getInstance(){
        Log.v("app"."getInstance instance "+instance.toString());
        return instance;
    }
    public void addParam(Object key,Object value){
        innerMap.put(key, value);
    }
    public Object getParam(Object key){
        returninnerMap.get(key); }}Copy the code