Singletons vs. Application Context in Android?

后端 未结 10 1430
醉话见心
醉话见心 2020-11-22 03:11

Recalling this post enumerating several problems of using singletons and having seen several examples of Android applications using singleton pattern, I wonder if it\'s a go

10条回答
  •  执笔经年
    2020-11-22 03:33

    From: Developer > reference - Application

    There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

提交回复
热议问题