ApplicationComponent.java
@Component(modules = SomeModule.class)
@ApplicationScope
public interface ApplicationComponent {
// stuff
ActivityComponent activityComponent();
}
ActivityComponent.java
@Subcomponent(modules = AnotherModule.class)
@ActivityScope
public interface ActivityComponent {
// stuff
void inject(MainActivity mainActivity);
}
SomeModule
can be overriden using something like this . But how about AnotherModule
?
One solution would be to separate the 2 components, but what if I want to reuse some bindings from the parent?
edit:
MainActivity.java
onCreate(Bundle bundle) {
getApplicationComponent().getActivityComponent().inject(this);
}
edit2:
ActivityRyle.java
init() {
application.setComponent(DaggerApplicationComponent.builder()
.someModule(new TestSomeModule(application))
.build();
}
edit3: I'm trying to avoid wiring too much stuff in Application
(where the main component is created).
You also just override the module.
Please keep in mind, how you create subcomponents:
public interface ApplicationComponent {
ActivityComponent activityComponent(/*needed modules go here*/);
}
So unless you have no-args constructors for modules, you have to put them as parameters in the method declaration.
If you want to be able to override modules with a no-arg constructor, you'd have to add them to your method signature:
public interface ApplicationComponent {
ActivityComponent activityComponent(AnotherModule module);
}
And in your test you just supply your subclass.
您需要将该模块声明为子组件工厂方法的输入参数。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.