This is chapter 1 of our series Mastering Android Context. We will cover basics of what context is and how many types of Context are present in Android.

Lets start with a simple question, what the heck is Context?

Before answering above question. Lets take a step back and discuss what is an app in Android. Simply put an app is a collection of few components you have written. These components could be Activity (UI), Service(Background), BroadcastReceiver (Action), ContentProvider(Data) and Resources(images, strings etc).

When a user installs an app in Android, actually she installs few components.

It is upto developer of those components to define the contract between component and system OR component and other components. Developer can choose to expose those components to system(using intent filter, example: send email, share picture) OR he can choose to expose components to only other components of his app. There are two ways for components to communicate, using Context and Intent. Latter also depends on context to play its role.

Dev Note- Thats how the Manifest is used by system. You define components there. Defining a component does not mean you exposed it to system. Component without intent-filter is like a class marked default which is accessible to your package OR using intent-filter is like making it public to make it world accessible with contract defined in intent filter. android.intent.category.LAUNCHER is the contract you expose to system and this component is shown in Launcher(Yes, you can have multiple launcher components).

Similarily Android operating system is also designed exposing components. Few well known are WifiManager, Vibrator, PackageManager etc.

Now coming back to Context, Simplest answer is Context is the bridge between components. You use it to communicate between components, instantiate components and access components. Read last line again. It may be between:

  • Your own components
  • Your component and a system component
  • Your own component and some other app component.

1. Your own components

Your activities are simple java classes which are instantiated and treated by the system in a special way. If you call constructor of your activity yourself you will face unexpected behavior. This is one of the most simple use case of Context. We use context to instantiate our components OR you can say to recall our components.

Another example of using context among your components is any component in need of Resources. You can access resources only if you have context. Yes there are some tweaks which we will cover in upcoming chapters.

Example (Here we are using activity as context):-


activity.startActivity(new Intent(activity, XYZ.class));


Intent someIntent = new Intent(android.content.Intent.CUSTOM_ACTION);

2. Your component and a system component

Suppose you want Wifi details in your app. WifiManager is the system component which offers wifi details. You can access WifiManager using context.getSystemService(Context.WIFI_SERVICE). Do you think it is well thought API? Our opinion is: nah! The casting and get everything from one God object is simply against OOPS basics. Yet it is what it is. We will make our peace with it.

Few services offered by context

2.1. Your component and a system component

Lets say everything in Android is component including File System offered by Android OS to apps. You can use context to access your apps file root directory and other offerings. Consider example from image below. Explaining directory type is out of scope of this article.

Few directory services offered by context

3. Your own component and some other app component

If you truly understand how Android component system works, there is no difference between point 1 and this. Communicating with your components and other app components is same if you have a clearly defined contract of your components and you use intent-filter mechanism (implicit intent). You can use context to communicate with other components. One catch is you have more control over your own components. Small example using email intent is given below.

 Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
 emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{address});
 emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
 emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);

 // Result is all email offering apps will be shown to user. 
 // She can pick what she likes/uses most.


Lets agree that everything in Android is a component. Context is the bridge between components. You use it to communicate between components, instantiate components and access components. I hope defination is made very clear and it will be easy for you to follow next chapters. In next chapter we will see types of Context.

Click here to read Chapter 2.