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
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.