What is 'Context' on Android?

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

    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: holds all Android Components.

    
         ..  
    
          .. 
    
         .. 
    
         .. 
     
    

    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.

提交回复
热议问题