What is 'Context' on Android?

前端 未结 30 2786
南旧
南旧 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

    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:

    • Loading common resources
    • Creating dynamic views
    • Displaying Toast messages
    • Launching Activities etc.

    Different ways of getting context:

    getContext()
    
    getBaseContext()
    
    getApplicationContext()
    
    this
    

提交回复
热议问题