如何在mainModule类中提供活动上下文?谢谢!代码如下:
@Module
public abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = {MainModule.class})
abstract MainActivity bindMainActivity();
}
Activtity builder:
@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;
}
}
我有一个appModule,以及每个活动的模块:
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;
}
}
AppClass:
public abstract class BaseActivity extends AppCompatActivity implements MvpView {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidInjection.inject(this);
}
}
我的所有活动都扩展了基础活动,注入了Dagger家属。
@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);
}
}
在mainModule中,我需要提供活动的上下文。
observation = np.asarray(observation[0]['vision'])
答案 0 :(得分:4)
解决方案是:
@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);
}
}
答案 1 :(得分:1)
对于遇到类似问题的人,我已经与Kotlin一起制作了一个项目,以及新的android-dagger
扩展程序,详细解释了这里的工作原理:https://github.com/Obaied/BareBonesAndroidDagger