In Android programming, what exactly is a Context
class and what is it used for?
I read about it on the developer site, but I am unable to understand it
Simple Example to understand context
in android :
Every boss has an assistant to look after, to do all less important and time-consuming tasks. If a file or a cup of coffee is needed, an assistant is on the run. Some bosses barely know what’s going on in the office, so they ask their assistants regarding this too. They do some work themselves but for most other things they need the help of their assistants.
In this scenario,
Boss – is the Android application
Assistant – is a context
Files/Cup of coffee – are resources
We generally call context when we need to get information about different parts of our application like Activities, Applications, etc.
Some operations(things where the assistant is needed) where context is involved:
Different ways of getting context:
getContext()
getBaseContext()
getApplicationContext()
this
Let's have a small analogy before diving deep in the technicality of Context
Every Boss has an assistant or someone( errand boy) who does less important and more time-consuming things for him. For example, if they need a file or coffee then an assistant will be on run. Boss will not know what is going on in the background but the file or the task will be delivered
So Here
Boss - Android Application
Assistant - Context
File or cup of coffee - Resource
Context is your access point for application-related resources
Let's see some of such resources or tasks
Launching an activity.
Getting an absolute path to the application-specific cache directory on the filesystem.
Determining whether the given permission is allowed for a particular process and user ID running in the system.
Checking whether you have been granted a particular permission.
And so on.
So if an Android application wants to start an activity, it goes straight to Context
(Access Point), and the Context
class gives him back the resources(Intent in this case).
Like any other class Context
class has fields and methods.
You can explore more about Context
in official documentation, it covers pretty much everything, available methods, fields, and even how to use fields with methods.
Context
means component (or application) in various time-period. If I do eat so much food between 1 to 2 pm then my context of that time is used to access all methods (or resources) that I use during that time. Content is a component (application) for a particular time. The Context
of components of the application keeps changing based on the underlying lifecycle of the components or application.
For instance, inside the onCreate() of an Activity
,
getBaseContext()
-- gives the context
of the Activity
that is set (created) by the constructor of activity.
getApplicationContext()
-- gives the Context
setup (created) during the creation of application.
Note: <application>
holds all Android Components.
<application>
<activity> .. </activity>
<service> .. </service>
<receiver> .. </receiver>
<provider> .. </provider>
</application>
It means, when you call getApplicationContext()
from inside whatever component, you are calling the common context of the whole application.
Context
keeps being modified by the system based on the lifecycle of components.
The class android.content.Context
provides the connection to the Android system and the resources of the project. It is the interface to global information about the application environment.
The Context also provides access to Android Services, e.g. the Location Service.
Activities and Services extend the Context
class.
Context is a reference to the current object as this. Also context allows access to information about the application environment.
A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. An Android app has activities. Context is like a handle to the environment your application is currently running in. The activity object inherits the Context object.
For more information, look in Introduction to Android development with Android Studio - Tutorial.