Dagger 2: Inject After Define Subcomponent
Solution 1:
@Subcomponent.Builder follows the same rules documented in @Component.Builder: You need to have a setter for every Module that Dagger can't initialize itself. (Modules are allowed to have constructor parameters or static factory methods, which would prevent Dagger from creating its own instance.)
Because Dagger can create a LoginModule by calling a zero-arg constructor, you can drop that method from the Builder; you don't need to recreate it. You might also consider making LoginModule's methods
static
, which would allow you to make LoginModule an interface or abstract class—then Dagger would be able to avoid holding a reference to the Module at all. Finally, for the simple@Provides
methods that bind two classes (e.g.@Provides B provideB(A a) { return a; }
) you can switch to@Binds
, which would allow the generated code to be even faster.At that point, your
@Subcomponent.Builder
becomes a subcomponent-specificProvider
: You can inject yourBuilder
or aProvider<Builder>
and get a new subcomponent instance by callingbuild()
on it. As long as you don't need to provide any Module instances, you don't need to have any other methods on your builder.You may be able to simply inject a
Provider<LoginComponent>
, but I haven't seen that in practice, and haven't tried it myself.To inject a LoginPresenter, you need to get to your AppComponent, create a new LoginComponent, and use it to inject your Activity by passing it in. However, this may be difficult, because you haven't given yourself any access to inject your LoginComponent.Builder.
Unless you want to try with dagger.android instead, you can add a method to call on your AppComponent to create a new LoginComponent.
// This way...LoginComponent createLoginComponent(); // or this way: LoginComponent.Builder createLoginComponentBuilder();
Sometimes you'll see the first way named
plus()
, which is a naming convention established in Dagger 1; that first way also lets you avoid creating a @Subcomponent.Builder or adding the subcomponent in yourAppModule
, though you might want to leave the structure the way you have it so you can add more to your AppModule later.To finish it off, in
Activity.onCreate
, you can have your Activity get your Application, access its AppComponent, and inject itself.((YourApplication) getContext().getApplicationContext()) .getApp() .createLoginComponent() .inject(this);
Post a Comment for "Dagger 2: Inject After Define Subcomponent"