我有一个 android 项目,其中有一个或多个 Activity 需要访问的不同对象,现在我正在考虑创建 Application
的子类,但是在 documentation 下应用程序声明如下:
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.
我的问题相当简单,最好的做法是使用静态单例类来包含所有对象吗?或者我是否正确地认为扩展应用程序是更好的选择?
最佳答案
为了回答您的问题,我将使用单例容器来访问这些对象,通过应用程序上下文使用上下文初始化该类(您很有可能需要上下文);但随后你会发现维护这些和单例容器有点困难。
为了解决这个对象图问题,我将使用一些 IoC:RoboJuice , AndroidAnnotations或Dagger真的很酷,而且他们提供了更多。他们每个人处理这个问题的方式都不同,但您不必担心。
希望对您有帮助!
关于java - 扩展应用程序还是使用单例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16972545/