What Is Context in Android?: Difficulty Level: Last Updated: 10 Sep, 2020

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

What is Context in Android?

 Difficulty Level : Medium


 Last Updated : 10 Sep, 2020
Android apps are popular for a long time and it is evolving to a greater level as user’s
expectations are that they need to view the data that they want in an easier smoother
view. Hence, the android developers must know the important terminologies before
developing the app. In Android Programming we generally come across a word context.
So what exactly is this context and why is it so important? To answer this question lets
first see what the literal meaning of context is:  
The circumstances that form the setting for an event, statement, or idea, and in terms of
which it can be fully understood
Looking at this definition we come across two things: 
 The context tells us about the surrounding information.
 It is very important to understand the environment which we want to
understand.
Similarly when we talk about the Android Programming context can be understood as
something which gives us the context of the current state of our application. We can
break the context and its use into three major points: 
 It allows us to access resources.
 It allows us to interact with other Android components by sending messages.
 It gives you information about your app environment.
In the official Android documentation, context is defined as:  
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.

Understanding Context by a Real World Example

Let’s a person visit a hotel. He needs breakfast, lunch, and dinner at a suitable time.
Except for these things there are also many other things, he wants to do during the time of
stay. So how does he get these things? He will ask the room-service person to bring these
things for him. Right? So here the room-service person is the context considering you
are the single activity and the hotel to be your app, finally, the breakfast, lunch &
dinner have to be the resources.
How Does This Work?
1. It is the context of the current/active state of the application.
Usually, the app got multiple screens like display/inquiry/add/delete screens(General
requirement of a basic app). So when the user is searching for something, the context is
an inquiry screen in this case.
2. It is used to get information about the activity and application.
The inquiry screen’s context specifies that the user is in inquiry activity, and he/she can
submit queries related to the app
3. It is used to get access to resources, databases, and shared preferences, etc.
Via Rest services, API calls can be consumed in android apps. Rest Services usually hold
database data and provide the output in JSON format to the android app. The context for
the respective screen helps to get hold of database data and the shared data across screens
4. Both the Activity and Application classes extend the Context class.
In android, context is the main important concept and the wrong usage of it leads to
memory leakage. Activity refers to an individual screen and Application refers to the
whole app and both extend the context class. 

Types of Context in Android


There are mainly two types of context are available in Android. 
1. Application Context and 
2. Activity Context

You might also like