What is 'Context' on Android?

前端 未结 30 3060
南旧
南旧 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:06

    If you want to connect Context with other familiar classes in Android, keep in mind this structure:

    Context < ContextWrapper < Application

    Context < ContextWrapper < ContextThemeWrapper < Activity

    Context < ContextWrapper < ContextThemeWrapper < Activity < ListActivity

    Context < ContextWrapper < Service

    Context < ContextWrapper < Service < IntentService

    So, all of those classes are contexts in their own way. You can cast Service and ListActivity to Context if you wish. But if you look closely, some of the classes inherit theme as well. In activity or fragment, you would like theming to be applied to your views, but don't care about it Service class, for instance.

    I explain the difference in contexts here.

提交回复
热议问题