Dagger 2在两个不同的片段中注入同一对象的两个实例

时间:2018-11-05 15:33:24

标签: android dagger-2

我试图将一个分页器适配器注入到两个不同的片段中,这两个片段不同时存在,但是每个视图分页器都有不同的对象。这是我尝试过的:

@Module
public class FragmentAModule {

    @Provides
    BaseFragment providesBaseFragment(FragmentA fragment) {
        return fragment;
    }

    @Provides
    CustomView[] providesArray(CustomView1 view1, CustomView2 view2, CustomView3 view3) {
        return new CustomView[]{view1, view2, view3};
    }

    @Provides
    PagerAdapter providePagerAdapter(CustomView[] array) {
        return new CustomPagerAdapter(array);
    }
}

@Module
public class FragmentBModule {

    @Provides
    BaseFragment providesBaseFragment(FragmentB fragment) {
        return fragment;
    }

    @Provides
    CustomView[] providesArray(CustomView4 view4, CustomView5 view5, CustomView6 view6) {
        return new CustomView[]{view4, view5, view6};
    }

    @Provides
    PagerAdapter providePagerAdapter(CustomView[] array) {
        return new CustomPagerAdapter(array);
    }
}

BaseFragment是扩展DaggerFragment的片段。 CustomView不是android.View,而是PagerAdapter中每个页面的控制器

匕首错误:

error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] android.support.v4.view.PagerAdapter cannot be provided without an @Provides-annotated method.
A binding with matching key exists in component: org.FragmentModule_ContributeFragmentB.FragmentBSubcomponent
A binding with matching key exists in component: org.MainActivityModule_ContributeFragmentA.FragmentASubcomponent
A binding with matching key exists in component: org.MainActivityModule_ContributeFragmentB.FragmentBSubcomponent
android.support.v4.view.PagerAdapter is injected at
org.FragmentA.adapter
org.FragmentA is injected at
dagger.android.AndroidInjector.inject(T)

2 个答案:

答案 0 :(得分:0)

看起来您可以使用多重绑定,您可以在官方文档https://sitepackagebuilder.com中了解更多信息。

另外,您可以尝试以下操作:

@Module
public class FragmentAModule {

    @Provides
    BaseFragment providesBaseFragment(FragmentA fragment) {
        return fragment;
    }

    @Provides
    @Named("array1")
    CustomView[] providesArray1(CustomView1 view1, CustomView2 view2, CustomView3 view3) {
        return new CustomView[]{view1, view2, view3};
    }

    @Provides
    @Named("array2")
    CustomView[] providesArray2(CustomView4 view4, CustomView5 view5, CustomView6 view6) {
    return new CustomView[]{view4, view5, view6};
}

    @Provides
    @Named("pagerAdapter1")
    PagerAdapter providePagerAdapter1(@Named("array1") CustomView[] array) {
        return new CustomPagerAdapter(array);
    }

    @Provides
    @Named("pagerAdapter2")
    PagerAdapter providePagerAdapter2(@Named("array2") CustomView[] array) {
        return new CustomPagerAdapter(array);
    }
}

,当您在活动中插入片段时,您将通过以下方式进行操作:

@Inject @Named("array1") CustomView[] array1;
@Inject @Named("array2") CustomView[] array2;

@Inject @Named("pagerAdapter1") PagerAdapter pagerAdapter1;
@Inject @Named("pagerAdapter2") PagerAdapter pagerAdapter2;

如果您不发表评论,希望对您有所帮助。

答案 1 :(得分:0)

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;

import javax.inject.Scope;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Scope
@Documented
@Retention(RUNTIME)
public @interface FragmentScope {
}

@Module
public class FragmentAModule {

    @Provides
    @FragmentScope
    BaseFragment providesBaseFragment(FragmentA fragment) {
        return fragment;
    }

    @Provides
    @FragmentScope
    CustomView[] providesArray(CustomView1 view1, CustomView2 view2, CustomView3 view3) {
        return new CustomView[]{view1, view2, view3};
    }

    @Provides
    @FragmentScope
    PagerAdapter providePagerAdapter(CustomView[] array) {
        return new CustomPagerAdapter(array);
    }
}

@Module
public class FragmentBModule {

    @Provides
    @FragmentScope
    BaseFragment providesBaseFragment(FragmentB fragment) {
        return fragment;
    }

    @Provides
    @FragmentScope
    CustomView[] providesArray(CustomView4 view4, CustomView5 view5, CustomView6 view6) {
        return new CustomView[]{view4, view5, view6};
    }

    @Provides
    @FragmentScope
    PagerAdapter providePagerAdapter(CustomView[] array) {
        return new CustomPagerAdapter(array);
    }
}

为了避免多重绑定,因为它不能很好地扩展并且需要我或其他工作人员记住每个id的去向,所以我只使用@Scope,这样只能在该范围内调用这些依赖项