gpt4 book ai didi

android - 如何提供具有 Dagger 依赖项的 Activity 上下文?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:09:38 25 4
gpt4 key购买 nike

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

    @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);
}

Activity 生成器:

@Module
public abstract class ActivityBuilder {

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

我有一个 appModule,每个 Activity 都有一个模块:

@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;
}
}

应用类:

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;
}
}

我所有的 Activity 都扩展了一个注入(inject) Dagger 依赖项的基础 Activity 。

public abstract class BaseActivity extends AppCompatActivity implements MvpView {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

AndroidInjection.inject(this);

}
}

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

@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);
}

}

最佳答案

解决方案是:

@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);
}

}

关于android - 如何提供具有 Dagger 依赖项的 Activity 上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46663967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com