Definitely makes you understand the context of Android

Source: Internet
Author: User

This question is stackoverflow above a hot question what's the Context in Android?
The purpose of finishing this article is that the context is really a very abstract thing, we will use it in the project, but many people simply do not understand what it is doing, this article will also add the context in Andorid–insight translation, Definitely let the reader understand the meaning of the context.

The usual, the author raises the question:
In Android, the context is exactly what the devil, what it is to make, I read a lot of documents, but can not clear the meaning of understanding it.

Answer:
Simply put, just like its name (context), he is an indicator of the current state of the project, and he can let the newly created, newly added object or component know the progress of the current project, in what state, so that we can easily understand the meaning of the context, The above is the status of what the project is already in, and the following is telling the object or component the current project is in a state.
You can get context contexts by getapplication (), GetContext (), Getbasecontext (), or this (while in the current activity).
Where the context is usually used:
When you create a new object:

1. Create a new view, adapter, listener

newnew SimpleCursorAdapter(getApplicationContext(), ...);

2. Get the resource file: For example Layout_inflater_service, sharedpreferences:

context.getSystemService(LAYOUT_INFLATER_SERVICE)   getApplicationContext().getSharedPreferences(*name*, *mode*);

3. Implicit access to components

getApplicationContext().getContentResolver().query(uri, ...);

If you don't understand, don't worry, now we start translating context in Andorid–insight

The context is probably one of the most common elements of the Android project.
Each document will have the following sentence:

"An Android context is a interface to global information about an application environment"
It means that the Android context is an interface for communicating the global information and programming environment

When I look for some articles about the context, I find that there are few, so I will do a few articles I see To do a collation.

The interface here is not the meaning of the interface interface in Java, this interface is the meaning of the connection, connecting two components.

More accurately, it is an entity that represents a variety of environmental data.

Android.content.context provides access to resources (resource files), databases, file systems, activity, system services, and other components.

Or it can be said that the context holds the state of the program, Access, permissions, file system, and other information, and is the activity, Service, application and other components of the parent class.

In the real world we can describe the context: it is like an access card, through this access card we can enter the warehouse, can enter the living room, can enter the kitchen to get the resources we want, this access card to a few separate rooms connected together.

A person with Access card (Context) can get resources for various systems.

We have 3 ways to get to the context:

    1. Mcontext = Getapplicationcontext ();
      The context that this approach obtains is the global context, which is unique and persistent throughout the life of the project, representing all activities context

    2. Mcontext = GetContext ()
      When the activity is destroyed, the context is then destroyed.

    3. Mcontext = Getbasecontext ();
      Honestly, I didn't use this way.

When to use Getapplicationcontext () or GetContext ()?

Through the above analysis we know that two context one is following the life cycle of the activity and one is following the life cycle of the application.

So, when you want to get a long lifecycle context, use application context, for example, when we want to use a system service, the cycle of this system service is longer than the life cycle of the activity, if we use GetContext () , when the activity is destroyed, the system service will not be able to proceed normally, this time we must use Getapplicationcontext ()

Definitely makes you understand the context of Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.