范围错误在Dagger中,ActivityScoped可能无法引用具有不同范围的绑定

时间:2019-04-30 17:10:52

标签: android dependencies dagger-2 code-injection dagger

我有一个带有不同片段的Activity,每个片段都有自己的ViewModel,Presenter,Etc ....所以我想做的是在片段范围内将ViewModel,Presenter,Etc ....将片段范围限制在托管活动范围内,但是我找不到找到该方法的方法,实际上我已经找到了,但是内存泄漏,所以我猜这不是正确的方法。

编译错误日志:

error: [Dagger/IncompatiblyScopedBindings] net.econcepts.di.modules.ActivityBindingModule_DashboardActivity.DashboardSubcomponent scoped with @net.econcepts.di.annotations.ActivityScoped may not reference bindings with different scopes:
@net.econcepts.di.annotations.FragmentScoped class net.econcepts.thawbi.cart.FragmentCart
@net.econcepts.di.annotations.FragmentScoped class net.econcepts.thawbi.cart.ViewModelCart
@net.econcepts.di.annotations.FragmentScoped class net.econcepts.thawbi.cart.adapter.AdapterCart
@net.econcepts.di.annotations.FragmentScoped @Binds net.econcepts.thawbi.cart.ContractCart.Presenter net.econcepts.di.submodules.CartSubModules.providesCartPresenter(net.econcepts.thawbi.cart.PresenterCart)
@net.econcepts.di.annotations.FragmentScoped class net.econcepts.thawbi.cart.PresenterCart [net.econcepts.di.component.AppComponent → net.econcepts.di.modules.ActivityBindingModule_DashboardActivity.DashboardSubcomponent]

依赖注入:

AppComponent.java:

@Singleton
@Component(modules = {
        BundleModule.class,
        ViewModelModule.class,
        ApplicationModule.class,
        ActivityBindingModule.class,
        AndroidSupportInjectionModule.class})
public interface AppComponent  extends AndroidInjector<BaseApplication> {


    @Component.Builder
    interface Builder{

        @BindsInstance
        AppComponent.Builder application(Application application);

        AppComponent build();
    }
}

ActivityBindingModule.java:

@Module
public abstract class ActivityBindingModule {

    @ActivityScoped
    @ContributesAndroidInjector(modules = {LoginSubModules.class})
    abstract LoginActivity loginActivity();


    @ActivityScoped
    @ContributesAndroidInjector(modules = {CartSubModules.class})
    abstract Dashboard dashboardActivity();
}

CartSubModules.java:

@Module
public abstract class CartSubModules {

    @FragmentScoped
    @ContributesAndroidInjector()
    abstract FragmentCart fragmentLogin();

    @FragmentScoped
    @Binds
    abstract ContractCart.Presenter
    providesCartPresenter(PresenterCart presenterCart);
}

还有

FragmentCart.java(标头):

@FragmentScoped
public class FragmentCart extends BaseFragment implements ContractCart.View 

AdapterCart.java

@FragmentScoped
public class AdapterCart extends RecyclerView.Adapter<AdapterCart.ViewHolder>

等...(ViewModel和Presenter也用FragmentScoped注释。

0 个答案:

没有答案