What is 'Context' on Android?

前端 未结 30 2943
南旧
南旧 2020-11-21 04:46

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

30条回答
  •  独厮守ぢ
    2020-11-21 05:09

    Boss Assistant Analogy

    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

    What official Android Developer site says about Context

    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.

提交回复
热议问题