如何在匕首2中制作子组件单例?

时间:2016-06-13 19:17:45

标签: android dagger-2

我想让我的子组件成为单身,这样我也可以让Login Presenter成为单身人士。这可能吗?

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
    LoginComponent getLoginComponent();
}

@Singleton
@Subcomponent(modules = LoginModule.class)
public interface LoginComponent {
}

public class LoginComponent {
    @Singleton
    LoginPresenter getLoginPresenter();
}

1 个答案:

答案 0 :(得分:3)

@Subcomponent无法成为@Singleton

虽然@Singleton规范对此有点模糊,但“单身”规范地表示“每个应用程序一个”。由于@Subcomponent是通过组件上的工厂方法创建的,因此单个子组件实例的单个子组件实例的方式是单子组件是子组件的单个组件及其工厂方法的仅为每个应用程序调用一次。实施该约束实际上是不可能的,因此该模式可能只是错误的来源。

相关问题