Context

The Context class represents global information about an application environment. This is abstract class whose implementation is provided by the Android system.

The ContextWrapper is proxying implementation of Context that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context. For example, the Application, Activity and Service classes are extend the ContextWrapper class.

Through context you can

  • get current state of the application
  • get information about the activity/service/application
  • 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.

When your object has the application lifecycle, then always use application context.

When your object has the activity lifecycle, then use activity context. It is better, then the application context, because allows to do any UI operations like showing toast, dialogs, and etc. Classes such as Fragment , View have a getContext() , which returns the context of the activity to which they belong.

Broadcast receivers and the content providers works with application context.

You can get application context from any other context by a getApplicationContext() method.

Wrong using context may cause memory leak. For example you hold reference to activity in object that has lifecycle beyound the activity lifecycle. User rotated device, and activity recreated. But your object will hold old instance of activity.