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

标签 android android-context

我正在开发一个项目,它有一个主模块(这里有 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/

相关文章:

android - 从应用程序类更改 Activity UI

android - 使用 ScrollViews 布局到 Horizo​​ntalScrollView

java - 使用套接字发送序列化的 Android 对象会抛出 InvalidClassException

android - OpenCV layout.xml 命名空间未绑定(bind)

安卓:SharedPreferences 问题

android - 如何在 Android 中执行点击操作后从通知发送 Toast?

android - 我们如何使用上下文类?

Android:getApplicationContext().getContentResolver() 与 getContentResolver() 之间的区别

java - Android 字符串中的回车符/换行符\n

Android:getApplicationContext() 在 View 子类中不起作用