使用@ContributesAndroidInjector时如何为SubComponent指定Scope

时间:2017-10-02 11:45:45

标签: android dagger-2 dagger

我试图通过from this blog post了解带有Dagger 2的全新Android注入器。我理解@ContributesAndroidInjector的概念,以及它如何避免重复代码,如博客

中所述

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

所以,如果你想让你的子组件处于不同的范围(比如@PerActivity),我们将如何实现这一点,因为我们根本没有创建子组件?

1 个答案:

答案 0 :(得分:2)

像这样:

@PerActivity
@ContributesAndroidInjector
abstract YourActivity yourActivity();

会生成如下内容:

@Subcomponent
@PerActivity
public interface YourActivitySubcomponent extends AndroidInjector<YourActivity> {
    @Subcomponent.Builder
    abstract class Builder extends AndroidInjector.Builder<YourActivity> {}
}