What is the Context in Android

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.

 

Definition of Context::

  • Context represents environment data
  • It provides access to things such as databases

Simpler terms ::

  • Consider Person-X is the CEO of a start-up software company
  • There is a lead architect present in the company, this lead architect does all the work in the company which involves such as database, UI etc
  • Now the CEO Hires a new Developer
  • It is the Architect who tells the responsibility of the new hired person based on the skills of the new person that whether he will work on Database or UI etc

Simpler terms ::

  • It’s like access of android-activity to app’s resource
  • It’s similar to a when you visit a hotel, you want breakfast, lunch & dinner in suitable timings right ?
  • There are many other things you like during the time of stay. How you get these things?
  • You ask the room-service person to bring the things for you
  • Here room-service person is the context considering you are the single activity and the hotel to be your app, finally the breakfast, lunch & dinner to be the resourcesThings that involve context are:
    1. Loading a resource.
    2. Launching a new activity.
    3. Creating views.
    4. obtaining system service.

Different invoking methods by which you can get context

  • getApplicationContext()
  • getContext()
  • getBaseContext()
  • or this (when in the activity class)

Example:

TextView TV=new TextView(this);

this -> refers to the context of the current activity.