0

I'm trying to understand the new New Android Injector with Dagger 2 from from this blog post. I understood the concept of @ContributesAndroidInjector, and how it avoids repetition of code as described in the blog

UI subcomponents(MainActivityComponent and DetailActivityComponent) are just like bridge in the graph. We don’t even have to use our brain to create this class.

So, if you want your subcomponents to be in a different scope (say @PerActivity), how would we achieve this, since we are not creating the sub-component at all?

1 Answers1

2

Like this:

@PerActivity
@ContributesAndroidInjector
abstract YourActivity yourActivity();

which will generate something like this:

@Subcomponent
@PerActivity
public interface YourActivitySubcomponent extends AndroidInjector<YourActivity> {
    @Subcomponent.Builder
    abstract class Builder extends AndroidInjector.Builder<YourActivity> {}
}
Benjamin
  • 7,055
  • 6
  • 40
  • 60
  • Okay, so this is as good as creating a SubComponent for `YourActivity` and putting a Scope to it? –  Oct 02 '17 at 11:52
  • @Jrd yes, see my updated answer with the code that Dagger will generate for you – Benjamin Oct 02 '17 at 11:54