简体   繁体   English

从辅助模块获取应用程序上下文

[英]Get application context from a secondary module

I am working on a project that has one main module(here I have the activities and the controllers..) and some secondary modules where I have some calendar and other implementations. 我正在开发一个项目,它有一个主要模块(这里我有活动和控制器......)和一些辅助模块,我有一些日历和其他实现。

In the main module I have a Application singleton class where I store the application context and I can get the app context statically from everywhere within my main module. 在主模块中,我有一个Application单例类,我存储应用程序上下文,我可以从主模块中的任何位置静态获取应用程序上下文。

The question is how can I make another application class in the secondary module? 问题是如何在辅助模块中创建另一个应用程序类? Currently I am using circular dependencies between the main module and the module where I want the app context, and I don't like too much to use this approach. 目前我在主模块和我想要应用程序上下文的模块之间使用循环依赖,我不太喜欢使用这种方法。

@David Wasser wrote: @David Wasser写道:

Why can't the code in the secondary modules call MainApp.getInstance() to get the application context? 为什么辅助模块中的代码不能调用MainApp.getInstance()来获取应用程序上下文? 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. 如果没有,则将单例应用程序上下文从主模块传递到辅助模块(作为方法调用中的参数或作为辅助模块中组件的构造函数中的参数。然后,您将不会在辅助模块中具有代码模块调用MainApp.getInstance() 。在任何情况下,您都不能拥有另一个应用程序类,因为只有一个应用程序类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM