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:
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
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?
Android system uses Activity Manager Service to manage the state(started, paused, resumed, etc) of all android components (activities, services, etc.)
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 (
LOCATION_SERVICEetc) as well.
I hope this short article clarifies the “why context” kind of questions you had in mind.
Did I get something wrong? Mention it in the comments. I would love to improve. 😊
Did you learn anything? Was it helpful? If yes, leave some claps 👏. It will make me happy 😊