Jrd Jrd - 3 years ago 247
Android Question

How to specify Scope for SubComponent when using @ContributesAndroidInjector

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?

Answer Source

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> {}
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download