Android Dagger 2 - 如何使自定义范围成为本地单例

j2e*_*nue 3 android dagger-2

根据这样的教程一个:在Dagger2我们能够提供使用自定义范围本地单一对象。

我已经有一个全局 appComponent,我的目标是创建一个活动范围,允许子组件拥有本地单例提供程序。

我的问题是当我创建一个自定义范围并两次注入我的活动时,我看到提供的对象不是同一个,即使我用自定义范围标记了它。对于使用 @Singleton 注释的 appComponent 来说,它虽然工作正常。让我们来看看我是如何做到的:

这是自定义活动范围:

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}
Run Code Online (Sandbox Code Playgroud)

这是我定义子组件的方式:

@ActivityScope
@Subcomponent(modules = {PresenterModule.class, UseCaseModule.class})
public interface ActivitySubComponent {
    void inject(LoginActivity target);
    void inject(LoginPresenter target);
    void inject(UCGetFireBaseAccount target);
}
Run Code Online (Sandbox Code Playgroud)

请注意,我已经使用上面自定义的 @ActivityScope 注释标记了这个子组件。现在让我们看看 appComponent,它是父组件和全局组件:

@Singleton
@Component(modules = {AppModule.class, NetworkModule.class, RepositoryModule.class})
public interface AppComponent {
    void inject(myappCloudRepo target);
    void inject (FireBaseCloudRepo target);
    ActivitySubComponent plus(PresenterModule presenterModule, UseCaseModule useCaseModule);
}
Run Code Online (Sandbox Code Playgroud)

注意 ActivitySubComponent 在这里被定义为一个子组件。

最后让我们看一下 PresenterModule 类,看看我想作为单例提供什么:

@Module
public class PresenterModule {

        @Provides
        @ActivityScope
        LoginPresenter provideLoginPresenter(Context context) {
        return new LoginPresenter(context);
    }
 }
Run Code Online (Sandbox Code Playgroud)

现在,当我实际使用它时,我将它设置在我的应用程序类中,如下所示:

public class MyApplication extends Application {

    private AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        appComponent = initDagger(this);
    }

    public AppComponent getAppComponent() {
       return appComponent;
    }

    protected AppComponent initDagger(MyApplication application) {
        return DaggerAppComponent.builder()
                .appModule(new AppModule(application))
                .build();
    }

    public ActivitySubComponent getActivityComponent() {
       return appComponent.plus(new PresenterModule(),new UseCaseModule());
    }
}
Run Code Online (Sandbox Code Playgroud)

然后最后在任何活动中我都这样做:

public class LoginActivity extends AppCompatActivity{

    ActivitySubComponent activityComponent;

    //this loginpresenter is from the activitysubcomponent
    @Inject
    LoginPresenter loginPresenter;

    //this retrofit object is from the appcomponent
    @Inject
    Retrofit retrofit;

   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       activityComponent= ((MyApplication)getApplication()).getActivityComponent();
       activityComponent.inject(this);

       Log.d("j2emanue",loginPresenter.toString());
       Log.d("j2emanue",retrofit.toString());


      activityComponent= ((MyApplication)getApplication()).getActivityComponent();
      activityComponent.inject(this);

      Log.d("j2emanue2",loginPresenter.toString());
      Log.d("j2emanue2",retrofit.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在注入 TWICE 后查看日志时,我希望所有对象都相同,但 loginPresenter 是另一个实例,但改造是相同的,因此可以正常工作。这是日志:

05-23 23:33:35.617 31298-31298/com.myapp.mobile.myappfashion D/j2emanue: com.myapp.mobile.myappfashion.UI.Presenters.LoginPresenter@1c27a460
05-23 23:33:35.617 31298-31298/com.myapp.mobile.myappfashion D/j2emanue: retrofit2.Retrofit@13366d19
05-23 23:33:35.617 31298-31298/com.myapp.mobile.myappfashion D/j2emanue2: com.myapp.mobile.myappfashion.UI.Presenters.LoginPresenter@3dc041de
05-23 23:33:35.617 31298-31298/com.myapp.mobile.myappfashion D/j2emanue2: retrofit2.Retrofit@13366d19
Run Code Online (Sandbox Code Playgroud)

请注意, loginpresenter 是另一个对象,因此它不会为我创建本地单例,只有在我取消引用活动子组件时才会销毁该单例。我在这里错过了什么?我希望它成为本地单例的原因是为了配置更改。通过这种方式,可以在配置更改期间维护演示者。

Dav*_*jak 5

在您的应用程序类中,您创建并返回一个新的子组件。

public ActivitySubComponent getActivityComponent() {
   return appComponent.plus(new PresenterModule(),new UseCaseModule());
}
Run Code Online (Sandbox Code Playgroud)

现在作用域意味着在一个作用域内一个对象存在一次。但在您的情况下,您创建了 2 个不同的组件。这两个组件将共享其父组件中的所有内容(它们是子组件),但它们范围内的任何内容都将被重新创建,但对于它们的范围来说是唯一
组件中的任何内容都将使用相同的带@ActivityScope注释的对象,但是如果您创建 2 个组件,您将拥有所有内容的 2 个副本。

如果你@Singleton举个例子,这并不意味着该对象将是一个实际的 Singleton。它是您的根组件应该拥有的范围的名称,它应该只创建一次并在应用程序的生命周期内保留。
如果您要创建 2 个 AppComponents,@Singleton您可以观察到相同的行为——对象的两个不同实例。


在您的示例中Retrofit是相同的,因为您AppComponent两次都使用相同的内容,但是LoginPresenter会随着ActivitySubComponent您创建的每个内容而重新创建。

使用 Dagger,您应该尝试让您的组件遵循与其作用域相同的生命周期,因此您的应用程序应该拥有一个AppComponent,并且每个 Activity 都应该有自己的ActivityComponent(将组件保留为成员变量!)。当你创建一个新的 Activity 时,你应该创建一个新的@ActivityScope作用域组件,但不要超过这个频率。

您应该getActivityComponent()从应用程序中删除您的并保留对您的 的引用ActivitySubComponent,因为使用相同的组件注入作用域依赖项将为您提供相同的对象。

activityComponent.inject(this);
activityComponent.inject(this);
// call it as many times as you'd like.
activityComponent.inject(this);
Run Code Online (Sandbox Code Playgroud)

只是不要重新创建您的组件。