我正在开发一个项目,它有一个主模块(这里有 Activity 和 Controller ......)和一些辅助模块,其中有一些日历和其他实现。
在主模块中,我有一个应用程序单例类,我在其中存储应用程序上下文,我可以从主模块中的任何地方静态获取应用程序上下文。
问题是如何在二级模块中创建另一个应用程序类?目前我在主模块和我想要应用程序上下文的模块之间使用循环依赖关系,我不太喜欢使用这种方法。
最佳答案
@David Wasser 写道:
Why can't the code in the secondary modules call MainApp.getInstance() to get the application context? Obviously the secondary module is dependent on the main module so I don't see how this is a circular dependency.
If not, then pass the singleton application context from the main module to the secondary modules (either as a parameter in method calls or as a parameter in the constructor of the component in the secondary module. Then you won't have code in the secondary modules calling
MainApp.getInstance()
. In any case you cannot have another application class as there is only one application class.
关于android - 从辅助模块获取应用程序上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12003260/