0

I'm doing what Jeff Bowman described at Dagger 2.10 Android subcomponents and builders and it works perfectly fine. Now I want to add abstract module with only @Binds How can I do this?

After I override seedInstance my activity is not visible inside that abstract module so I probably should add something to seedInstance

Community
  • 1
  • 1
RCH
  • 1,247
  • 1
  • 11
  • 16

1 Answers1

3

If you your module is an abstract class or interface, or it has a public parameterless constructor, you can just add it to your module list without supplying an instance and everything should work just fine. This means:

@Subcomponent(modules = {YourAbstractModule.class /*, ... */})
public interface YourActivitySubcomponent extends AndroidInjector<YourActivity> {

or using @ContributesAndroidInjector:

@ContributesAndroidInjector(modules = {YourAbstractModule.class /*, ... */})
@YourScope
abstract YourActivity bindYourActivityInjector();
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • I'm getting: Error:(8, 8) error: [dagger.android.AndroidInjector.inject(T)] com.rachapps.testapp.activity.MainActivity cannot be provided without an \@Inject constructor or from an \@Provides-annotated method. This type supports members injection but cannot be implicitly provided. com.rachapps.testapp.activity.MainActivity ... – RCH May 10 '17 at 08:02
  • 1
    By [overriding `seedInstance`](https://github.com/RadoslawChmielewski/TempRep/blob/master/app/src/main/java/com/rachapps/testapp/activity/MainSubComponent.java#L20), Dagger no longer counts it as `@BindsInstance`, so MainActivity isn't available to be injected in your graph. You're getting that message because your binding in MainModule looks to Dagger like you might be asking it to create a MainActivity for you, which it can't do. You'll need to make it available through a module, or by defining a different `@BindsInterface abstract` method. – Jeff Bowman May 10 '17 at 09:32
  • Thanks for your help! – RCH May 10 '17 at 12:32