我有一个使用 Dagger 的 Android 应用程序。我想在整个应用程序的某些部分为共享公共(public)范围的多个 Activity 添加范围对象图。以下模块位于根 ObjectGraph 中
@Module(
injects = {
MyApplication.class,
},
complete = false,
library = true)
public class BasicContextManagerModule {
private Context applicationContext;
public BasicContextManagerModule(Context applicationContext) {
this.applicationContext = applicationContext;
}
@Provides
Context getApplicationContext() {
return applicationContext;
}
}
然后我尝试通过existingObjectGraph.plus(new FileManagerModule())添加以下模块;
@Module(
injects = {
MyListActivity.class,
MyFileDetailActivity.class,
MyFileInfoActivity.class,
},
includes = BasicContextManagerModule.class
)
public class FileManagerModule {
@Provides
FileManager provideFileManager(Context context) {
return new FileManager(context);
}
}
但是结果是
java.lang.UnsupportedOperationException: No no-args constructor com.myapp.core.modules.BasicContextManagerModule$$ModuleAdapter
有人可以帮助我理解为什么 plus 不允许这样做吗?我从 dagger 文档中读到,plus 扩展了对象图,您可以包含并添加模块。但我没能实现这一点。
最佳答案
includes
表示该模块将位于同一个子图中,如果您不传递实例,Dagger 将实例化它。
addsTo
表示引用的模块预计位于图表中(实际上位于父图表中),但 Dagger 不会为您提供它。
你想要的是addsTo
。
关于android - Dagger ObjectGraph plus() 包含根模块中的模块的模块不断返回 无无参数构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25391259/