On the Android Context

1. Background

Android as a novice, every time I see a Context as a parameter, a little anxious, sometimes you can pass this, sometimes it does not work, do not know why you can, why can not the root cause of the Context is still scanty, but why Context is used as a parameter in many places, has a smattering of knowledge is not the way to go, it is necessary to look at what Context really is.

2. What is the Context?

Context Chinese meaning is the context in primary school language class, we certainly understand the words contact contacted the context of the topic, "context" refers to the context, the meaning of our context is certainly better understanding, one primary brush so many questions, the second is often encountered in real life, when you have a crush on to say to you, you're a good man, you have to extrapolate, depending on their specific situation , this sentence is really praise you or you politely declined, then the program whether in the development of "context" of semantics and language classes in the "context" of the semantics of the same?

The answer is the same, in the program, the context refers to the application / object current state, such as C language, a simple section of c = a + b code to be able to run up, first of all a and b have the assignment, a and b the assignment, the assignment state of a and b is c = a + b can run in the context, and a and b assignment operation c = a + b can not directly run to run up and have the main function, the main function operation the state is the main function in the context of each code to run.

In the C language, we do not need special tangled in the context of this concept, it does not affect write code, but there is also the context of the concept of the so-called Book of Changes says "people daily and I do not know," but on the Android platform, especially as the introduction of Context parameters, also the place with the particularly large number of which have to tangle Context concept.

Why Android in need Context as a parameter?

I understand that for programmers. For example, one of the four components Activity, is under the complete engineering environment has a good package, during the packaging process has been considered adapting the operating environment, management component lifecycle, the scheduling of resources such as page layout, these so-called context, in the context of this background, the programmer only needs to inherit, you can add your own code inside, let the program up and running, without having to write from scratch. Continue to use the C language example above to understand is that we assume that the system helped write the main function, which is a package context, we do not have to write your own main plus our code up and running. This is why the four components of the Android system is not new directly, because it is not a simple object, but need to have a context, can only inherit been packaged.

Context as a parameter so what role? This is the official definition given in the Context of Google has been winning numbers suggests:

Interface with global information about the application environment. This is an abstract class, which is implemented by Android. It allows access to application-specific resources and classes, as well as performing the application-level operations (e.g. start Activity, broadcast reception and the like Intent).

To be honest, although the official definition of a clear definition of the Context function, according to this definition, easily understood Activity start since Activity must to start at a certain context, which has already been explained, but this definition did not explain why Context able to access resources.

Right ah, Why?

My understanding is, Android Context is equivalent to the current assignment system context in the Context of this parameter, which tells us the current environment in the program, which piled into the environment from a variety of resources, and thus be able Context access to resources and the environment behind.

3.Context class inheritance graph

Context inheritance graph
We look at the inheritance Context can be seen from the chart, there are two specific category under Context, which is ContextWrapper wrapper class, ContextImpl is achieved, a package of classes are responsible for beautiful flower, ContextImpl responsible breadwinner, means ContextWrapper substantially resealing Context class methods, and methods which are used, is achieved by ContextImpl, this design pattern can speak and to classify the interface.

ContextWrapper inherited by the three sub-categories, two of which are old acquaintances Application and Service, the other one is ContextThemeWrapper class, as the name suggests, this class contains related topics (Theme) interface, while the class is inherited ContextThemeWrapper Activity, because the interface Activity assembly required theme, Application and Service is no interface, no need to catch and ContextThemeWrapper relations.

Can also be seen from the inheritance graph, Context have Application, Service and Activity three species, their use is still a difference, the Internet has a chart (below) for three different types of Context used to summarize
Context scenarios
the original table with a note, no mean some places is actually possible, but not recommended, is not here to write a note, according to table proposals directly.

The table can be seen, Context function Activity category of the most complete, and UI related options Activity class to be sure, after all, is the direct successor to the theme of class Context, is the only four major components of the interface, in addition to UI, Application class can operation, programmers now often develop a global Application Context as a general tool and probably because of this, but remember not to operate UI.

Other tables mixed Content Provider and BroadcastReceiver, see Context inheritance graph is not two goods ah, this is because the internal method of the two goods are in a context.

Finally, as a white, try side by side written understanding, may be biased understanding of Context, any errors, please correct me.

参考目录
1.https://www.cnblogs.com/chenxibobo/p/6136693.html
2.https://stackoverflow.com/questions/3572463/what-is-context-on-android#
3.https://my.oschina.net/youranhongcha/blog/1807189

Published 11 original articles · won praise 4 · Views 2370

Guess you like

Origin blog.csdn.net/lansoul1987/article/details/104486383