0

如何注入演示使用Dagger2如何使用Dagger2

片段我已經寫了下面的代碼

@Module 
public abstract class ActivityBuilder { 

    @ContributesAndroidInjector(modules = { DetailCastActivityModule.class, FragmentDependencyProvider.class }) 
    abstract DetailCastActivity bindDetailCastActivity(); 
} 

@Module 
public abstract class FragmentDependencyProvider { 

    @ContributesAndroidInjector(modules = CastInfoFragmentModule.class) 
    abstract CastInfoFragment provideCastInfoFragmentFactory(); 
} 

@Module 
public class CastInfoFragmentModule { 

    @Provides 
    CastInfoMvpPresenter<CastInfoMvpView> provideCastInfoMvpPresenter(CastInfoPresenter<CastInfoMvpView> presenter) { 
     return presenter; 
    } 

} 

,但我仍然得到以下錯誤,即使我已經寫了提供注入演示的片段方法

Error:(24, :sunglasses: error: [dagger.android.AndroidInjector.inject(T)] com.app.nmot.ui.castdetail.info.CastInfoPresenter cannot be provided without an @Provides- or @Produces-annotated method. 
com.app.nmot.ui.castdetail.info.CastInfoPresenter is injected at 
com.app.nmot.ui.castdetail.info.CastInfoFragment.presenter 
com.app.nmot.ui.castdetail.info.CastInfoFragment is injected at 
dagger.android.AndroidInjector.inject(arg0) 

評論中提到的答案沒有解決我的問題。 我已經檢查了所有的註釋和我使用的Dagger2

+0

的可能重複的構造函數添加@Inject [我如何解決Dagger 2錯誤'...不能提供\ [... \]'?](https://stackoverflow.com/questions/44912080/how-doi-i-fix-dagger-2-錯誤 - 不能提供) –

+0

這個解決方案不適合你嗎? https://stackoverflow.com/questions/36838898/presenter-injection-with-dagger-2?answertab=votes#tab-top –

+0

@MaximeJallu - 不,我已檢查所有註釋,我使用新的android注射器提供Dagger2 – Passiondroid

回答

1

1提供新的Android噴油器創建範圍

import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 

import javax.inject.Scope; 

@Scope 
@Retention(RetentionPolicy.RUNTIME) 
public @interface BaseScope { 
} 

2 - 創建您的合同

public interface FeatureContract { 
    interface View { 
     void onReceiveError(Throwable throwable); 
     void onReceiveItems(List<Object> items); 
     void showAlertDialog(); 
     ... //others methods 
    } 

    interface Presenter { 
     void onInitView(Object item); 
    } 
} 

3-創建模塊(dagger2)

import dagger.Module; 
import dagger.Provides; 

@Module 
public class FeatureContractModule { 

    private final FeatureContract.View mView; 

    public FeatureContractModule(FeatureContract.View view) { 
     mView = view; 
    } 

    @Provides @BaseScope 
    FeatureContract.Presenter providesFeaturePresenter(FeatureContract.View view) { 
     return new FeaturePresenter(view); 
    } 

    @Provides @BaseScope 
    FeatureContract.View providesFeatureView() { 
     return mView; 
    } 
} 

4-創建您的演示者

public class FeaturePresenter implements FeatureContract.Presenter{ 

    @NonNull 
    private final FeatureContract.View mView; 

    public FeaturePresenter(@NonNull FeatureContract.View view){ 
     mView = view; 
    } 

    @Override 
    public void onInitView(Object item){ 
     mView.showAlertDialog(); //<--for sample 
    } 
} 

5在你的片段

import javax.inject.Inject; 

public class FeatureFragment extends Fragment implements FeatureContract.View{ 
@Inject FeatureContract.Presenter mPresenter; 

@Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     ((MyApplication) getActivity().getApplication()).getDataComponent() 
                   .plus(new FeatureContractModule(this /*view*/)) 
                   .inject(this /*fragment*/); 

     mPresenter. onInitView(null); 
    } 
} 

5-你的應用

public class MyApplication extends Application { 
    //Dagger object 
    private DataComponent mDataComponent; 

    /** 
    * Dagger Injector 
    */ 
    public static DataComponent get(Context context) { 
     MyApplication application = (MyApplication) context.getApplicationContext(); 
     return application.mDataComponent; 
    } 

    @Override 
    public void onCreate() { 
    super.onCreate(); 
    mDataComponent = DaggerDataComponent.builder() 
             .dataModule(new DataModule(this, Locale.getDefault())) 
             .build(); 
    } 
    public DataComponent getDataComponent() { 
     return mDataComponent; 
    } 
} 

6 - 創建DataComponent

import javax.inject.Singleton; 

import dagger.Component; 

@Singleton 
@Component(modules = {DataModule.class}) 
public interface DataComponent { 
Application application(); 
FeatureComponent plus(FeatureContractModule module); 
... 
} 

7 - 最後的ComponentModule

import dagger.Module; 
import dagger.Provides; 

@BaseScope 
@Subcomponent(modules = FeatureContractModule.class) 
public interface FeatureComponent { 
    void inject(FeatureFragment fragment); 
} 

我相信我還沒有忘記任何東西

+0

請參閱博客:[https://android.jlelse.eu/android-mvp-architecture-with-dependency-injection-dee43fe47af0](MVP_Dagger2 Article1) 或者:[https://www.raywenderlich.com/146804/依賴注入匕首-2](MVP_Dagger2 Article2) –

0

讓您CastInfoFragmentModule摘要和補充一點:

@YourScope 
@Binds 
abstract CastInfoMvpPresenter<CastInfoMvpView> bindPresenter(CastInfoPresenter<CastInfoMvpView> presenter); 

而且,你必須在CastInfoPresenter

+0

我已經有@Inject CastInfoPresenter的構造函數。你能告訴我什麼是自定義範圍的需要嗎? – Passiondroid

+0

因此,您總是在組件的生命週期中獲得相同的對象 – Benjamin