我如何提供匕首依赖的活动上下文?

我如何在mainModule类中提供活动上下文? 谢谢! 代码如下所示:

@Singleton @Component(modules = {AndroidInjectionModule.class, AppModule.class, ActivityBuilder.class}) public interface AppComponent { @Component.Builder interface Builder { @BindsInstance Builder application(Application application); AppComponent build(); } void inject(MvmApp app); } 

活性建造者:

 @Module public abstract class ActivityBuilder { @ContributesAndroidInjector(modules = {MainModule.class}) abstract MainActivity bindMainActivity(); } 

我有一个appModule,并为每个活动模块:

 @Module public class AppModule { @Provides @Singleton @ApplicationContext Context provideContext(Application application) { return application; } @Provides @Singleton DataManager provideDataManager(AppDataManager appDataManager) { return appDataManager; } @Provides @DatabaseInfo String provideDatabaseName() { return "carDatabase"; } @Provides @Singleton AppDataBase provideAppDatabase(@DatabaseInfo String dbName, @ApplicationContext Context context) { return Room.databaseBuilder(context, AppDataBase.class, dbName) .build(); } @Provides @Singleton DbHelper provideDbHelper(AppDbHelper appDbHelper) { return appDbHelper; } } 

AppClass:

 public class MvmApp extends Application implements HasActivityInjector { @Inject DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector; @Override public void onCreate() { super.onCreate(); DaggerAppComponent.builder() .application(this) .build() .inject(this); } @Override public DispatchingAndroidInjector<Activity> activityInjector() { return activityDispatchingAndroidInjector; } } 

我所有的活动扩展了一个注入Dagger依赖的基本活动。

 public abstract class BaseActivity extends AppCompatActivity implements MvpView { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidInjection.inject(this); } } 

在mainModule中,我需要提供活动的上下文。

 @Module public class MainModule { @Provides MainMvpPresenter<MainMvpView> provideMainPresenter( MainPresenter<MainMvpView> presenter) { return presenter; } @Provides CompositeDisposable provideCompositeDisposable() { return new CompositeDisposable(); } @Provides CarAdapter provideCarAdapter( @ActivityContext Context context) { return new CarAdapter(context); } } 

    加( 匕首2.11

     @Provides public Activity provideActivity(BaseActivity baseActivity) { return baseActivity; } 

    和你的CarAdapter

     @Provides CarAdapter provideCarAdapter(Activity context) { return new CarAdapter(context); } 

    到你的模块,应该这样做。 请记住这一点

      AndroidInjection.inject(this); 

    必须先调用super.onCreate(savedInstanceState);

    解决方案是:

     @Module public class MainModule { @Provides MainMvpPresenter<MainMvpView> provideMainPresenter( MainPresenter<MainMvpView> presenter) { return presenter; } @Provides CompositeDisposable provideCompositeDisposable() { return new CompositeDisposable(); } @Provides CarAdapter provideCarAdapter(MainActivity activity) { return new CarAdapter(activity); } } 

    对于面临类似问题的人,我已经用Kotlin做了一个项目,并且在这里有一个关于如何工作的漫长解释的新的android-dagger扩展: https : //github.com/Obaied/BareBonesAndroidDagger