问题
I follow this guide for making sub components for my Android application. Here I defined one sub component named LoginComponent
used for LoginActivity:
@Subcomponent(modules = LoginModule.class)
public interface LoginComponent {
void inject(LoginActivity activity);
@Subcomponent.Builder
interface Builder {
Builder requestModule(LoginModule module);
LoginComponent build();
}
}
@Module
public class LoginModule {
@Provides
LoginManager provideLoginManager(LoginManagerImpl manager) {
return manager;
}
@Provides
LoginView provideLoginView(LoginViewImpl view) {
return view;
}
@Provides
LoginPresenter loginPresenter(LoginView view, LoginManager manager) {
return new LoginPresenterImpl(view, manager);
}
}
I define this sub component inside another component:
@Module(subcomponents = LoginComponent.class)
public interface AppModule {
}
@Singleton
@Component(modules = {
AppModule.class
})
public interface AppComponent {
}
Here is my LoginActivity:
public class LoginActivity extends AppCompatActivity {
@Inject LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
My question is:
- purpose of
@Subcomponent.Builder
. I don't understand this point, because all modules in this component has been defined on@Subcomponent
annotation. Why should we duplicate again. - how can I injected LoginPresenter on above activity.
Thanks
回答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);
来源:https://stackoverflow.com/questions/43214994/dagger-2-inject-after-define-subcomponent