Context of the Context — Android

Because context matters.

As Android developers, we have to deal with lots of contexts, the context of code written, the context of features being developed, or “The Context” which we have to pass in every here and there to satisfy some code looking for “The Context”.

I have always struggled to explain and even have a core understanding of context in android for many many years. In this short article, I will try my best to explain the context behind the context in a very simple way possible.

By the book, The Context is a:

By the book definition, we now know this is an abstract class and its implementation is provided by the Android system itself. Why do we need it? Because it allows access to application-specific resources and it also allows application-level operations such as activities, broadcasting and receiving intents, etc. very simple and straightforward.

But why do we need context to launch an activity? Or why do we need it to access application or even system resources?

Because…

Android system uses Activity Manager Service to manage the state(started, paused, resumed, etc) of all android components (activities, services, etc.)

Therefore, consider Context as a bridge between your Android application and the Android system. Because its implementation (ContextImpl) is provided by the android system itself therefore we use context to launch activities (states of which are managed by AMS which is a system service) and we use it to access system-level services (NOTIFICATION_SERVICE, LOCATION_SERVICEetc) as well.

I hope this short article clarifies the “why context” kind of questions you had in mind.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store