Analyze the relationship and difference of Context in Application, Activity and Service from source code

Analyze the relationship and difference of Context in Application, Activity and Service from source code


Let's briefly review what is Context?

Context is translated into context, which is the global information interface of the application environment. It is an abstract class that implements the ContextImpl class provided by the Android system. Through Context, we can easily access the resources and classes of the application, and perform various operations at the application level, such as starting Activity and Service, registering and sending broadcasts, permission operations, theme operations, storage object operations, and so on. It is simply a universal class! No wonder we need it so often.

Context and Context family, please refer to "From the Context family tree to talk about the design patterns used by the Context family?" " .

In actual development, we can divide Context into three types: Application, Activity, and Service from the perspective of usage scenarios. This article will start from the usage scenario and introduce the process of creating and using Context and the connections and differences between them.

Finally, let's think about another question: How many Context objects does an application have during its running?

Context in Application

Let's first look at the creation and assignment process of Context in Application.

The assignment process of mBase

Let's look at the definition of Application:

 

Guess you like

Origin blog.csdn.net/u011578734/article/details/113757977