What is a context on Android?

Question:

What is a context on Android?

What's the difference between getContext() , getApplicationContext() , getBaseContext() ?

Is it related to getActivity?

Answer:

(References: official documentation and two SOen questions .)

Context

It is an access point for global information about an application 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 starting activities , sending or receiving broadcast intents , etc.

In other words, Context is the system-provided way for your application to access certain resources (such as the ability to start an activity , start or stop a service , send a broadcast , open a database or preferences file, etc.) or classes (such as the different Managers that the system offers: Telephony, Alarm, Sensors, Audio, Notifications, Energy, USB, etc.). These resources and classes have the particularity of being global at the application level, that is, they are application level. It's still a god object as Square says – and, in fact, without a Context instance you don't do much in an Android app.

Application

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

Activity

An activity is a single, focused thing that a user can do.

Service

It's an application component that can both 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.

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

Differences between methods

  • View.getContext() : Returns the context in which the view is being displayed. Usually the Activity active.

  • Activity.getApplicationContext() : Returns the entire application context (the process within which all Activities are running). Use this in place of the currently active Activity context if you are in need of a context tied to the entire application lifecycle.

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

  • Fragment.getActivity() : Returns the Activity to which this fragment is attached. A fragment does not have a Context by itself; however, when it is attached or added to an Activity has access to the context of that Activity , or if it saves an Application instance, it has access to that instance even if it is disconnected from the Activity .

Scroll to Top