While developing Android applications, we all see and use the term context. But for most new programmers its a nightmare to resolve a context related error. This post helps you understand what context really means in Android.
What exactly is Context? Well, the documentation itself provides a rather straightforward explanation: The Context class is an “Interface to global information about an application environment.” The Context class itself is declared as abstract class, whose implementation is provided by the Android OS. The documentation further provides that Context “…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.” You can understand very well, now. The Context provides the link or hook, if you will, for an Activity, Service, or any other component, thereby linking it to the system, enabling access to the global application environment. In other words: the Context provides the answer to the components question of “where the hell am I in relation to app generally and how do I access/communicate with the rest of the app?”
You only “have” the context inside of your Activity because your activity itself inherits from Context. Context is context of current state of the Application/Object and provides information about the Actvity or Application to newly created components. Relevant Context should be provided to newly created components (whether Application Context or Activity Context).
Different ways of getting context
- View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.
- Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.
- ContextWrapper.getBaseContext(): If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().
- or this (when in the Activity class or usually the currently active Activity), but “this” and getContext() both are same.
Difference between Activity Context and Application Context:
They are both instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.
If you read the docs at getApplicationContext it notes that you should only use this if you need a context whose lifecycle is separate from the current context.
But in general, use the activity context unless you have a good reason not to.
I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
Latest posts by durga chiranjeevi (see all)