What is a context on Android?

16

What is a context on Android?

What is the difference between getContext() , getApplicationContext() , getBaseContext() ?

Is it related to getActivity?

    
asked by anonymous 01.11.2014 / 00:07

2 answers

34

(References: official documentation and two SOAP questions.)

Context

  

It is an access point for global information about a   environment. It is an abstract class whose   implementation is provided by the Android system. It allows access to   application-specific features and classes, as well as calls to   application-level operations such as launching    activities , send or receive intents by broadcast , etc.

In other words, Context is the system-provided way for your application to access certain features (such as the activity feature), start or stop a service , send a broadcast , open a database or preferences file, etc.) or classes (like the various%% of the system that the system offers: Telephony, Alarm, Audio, Notifications, Power, USB, etc). These features and classes have the particularity of being global at the application level, that is, they are application level. It is still a god object as Square - and, in fact, without an instance of Managers you do not do much in an Android application.

Application

  

Base class for those who need to preserve overall application state.   You can provide your own implementation (there is usually no   need, and static singletons can provide the same   functionality in a more modular way).

Activity

  

An activity is a unique and focused thing the user can do.

Service

  

It is an application component that can either represent an application's desire to perform a longer duration operation while avoiding interacting with the user or providing functionality for other applications to use.

Context , Application , and Activity are Service concretions, that is, concrete classes that implement Context of Android. When you extend these classes into your code, you have access to the application-level services provided by Context inherited by these classes. In addition, you have access to resources specific to each subclass; for example,% active% can currently be closed via the Context method and can execute code on the main thread in a simple way using the Activity method. finish() , runOnUiThread() and Activities each have their own particular life cycle. And contexts are not always interchangeable; for example, if you try to display a Services passing it a Applications context, this will cause an error because Android expects a Dialog to be passed. (This problem is more of an Android idiosyncrasy than should expect to receive a Application soon.)

Differences between methods

  • Activity : Returns the context in which the view is displayed. Usually the Activity active at the moment.

  • View.getContext() : Returns the context of the entire application (the process within which all Activities are running). Use this instead of the active Activity context at the moment if you are in need of a context bound to the entire application lifecycle.

  • Activity : This is a method of class Activity.getApplicationContext() . And ContextWrapper.getBaseContext() is "a proxy implementation of ContextWrapper that simply delegates all your calls to another ContextWrapper . It can be extended to modify behaviors without changing the Context original."

  • Context : Returns the Context to which this snippet is appended. A fragment does not have Fragment.getActivity() on its own; however, when attached ( attached or added ) a Activity has access to the context of this Context , or else an instance of Activity is stored access to this instance even though it is disconnected from Activity .

03.11.2014 / 23:38
2

As the name implies, it is the context of the object or application. This is a way to access the current state of the application in the code.

On Android, Context is the base class for Activity , Service , Application , so it's a way to access and deal with your application via code.

    
03.11.2014 / 21:56