It helps you understand the Context in Android and androidcontext.
This is a hot question in StackOverFlow. What is Context in Android?
The purpose of this article is that Context is indeed a very abstract thing and we will use it easily in projects. However, many people simply do not understand what it is, this article will also add a translation of Context in Andorid-INSIGHT to help readers understand the meaning of Context.
Old rules:
In Android, Context is a ghost thing. What does it mean? I read many documents, but I cannot clear the meaning of Context.
Answer:
Simply put, like its name (context), it is an identifier of the current state of the Project, which can be created, the newly added object or component knows the progress of the current project and in what state, so that we can easily understand the meaning of the context, the preceding figure shows the status of the previous project. The following figure shows the status of the object or component in the current project.
You can use getApplication (), getContext (), getBaseContext (), or this (in the current Activity) to obtain the context.
Generally, Context is used:
When creating a new object:
1. Create a New View, adapter, and listener
TextView tv = new TextView(getContext());ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
2. Obtain the resource file: for example, LAYOUT_INFLATER_SERVICE, SharedPreferences:
context.getSystemService(LAYOUT_INFLATER_SERVICE) getApplicationContext().getSharedPreferences(*name*, *mode*);
3. Implicit Component Access
getApplicationContext().getContentResolver().query(uri, ...);
If you do not understand it, do not worry. Now we start to translate Context in Andorid-INSIGHT
Context is probably the most common element in Android projects.
Each document contains the following sentence:
"An android context is an interface to global information about an application environment"
Android context is an interface for communicating global information and program environment.
When I look for some articles about context, I find that there are few articles, so I will sort out some of the articles I have seen.
The interface here does not mean the interface in java. This interface means the connection and connects two components.
More accurately, it is an entity that represents various environmental data.
Android. content. context enables resources (resource files), databases, file systems, activities, system services, and other components to have accessible portals.
You can also say that Context holds the state, entry, permission, file system, and other information of the program, and is the parent class of components such as Activity, Service, and Application.
In the real world, we can describe Context as follows: it is like a access card. With this access card, we can enter the warehouse, the living room, and the kitchen to get the resources we want, this access card connects several separate rooms.
A person (Activity) with a Context can obtain resources of various systems.
We have three methods to obtain context:
When Will getApplicationContext () or getContext () be used ()?
Through the above analysis, we know that one of the two contexts follows the activity lifecycle and the other follows the application lifecycle.
Therefore, when you want to obtain a context with a long life cycle, use Application Context. For example, if we want to use a system service, this system service has a longer life cycle than the activity. If getContext () is used, the system service cannot be normally executed when the activity is destroyed, in this case, we need to use getApplicationContext ()