Today I want to talk about Context in Android. As you know, whether we’re attaching a view or get an asset, – almost always there is a reference to Context. From the documentation, Context is an 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.
On the one hand, we all know it and use it, on the other – we often use it in a wrong way or just don’t care enough whether garbage collector will clean it or will the context be hanging there somewhere or, even worse, cause a memory leakage. So, what are the ways of getting a context? A couple actually:
- this / getActivity()