What is 'Context' on Android?

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

    Putting simple, Androids Context is a mess that you won't love until you stop worrying about.

    Android Contexts are:

    • God-objects.

    • Thing that you want to pass around all your application when you are starting developing for Android, but will avoid doing it when you get a little bit closer to programming, testing and Android itself.

      • Unclear dependency.

      • Common source of memory leaks.

      • PITA for testing.

    • Actual context used by Android system to dispatch permissions, resources, preferences, services, broadcasts, styles, showing dialogs and inflating layout. And you need different Context instances for some separate things (obviously, you can't show a dialog from an application or service context; layouts inflated from application and activity contexts may differ).

提交回复
热议问题