gpt4 book ai didi

java - 如何将 Activity 实例发送到 dagger2 构造函数中的模块

转载 作者:行者123 更新时间:2023-12-02 13:13:08 25 4
gpt4 key购买 nike

我在我的应用程序中使用dagger2。我已经创建了整个应用程序中的模块和组件,因此我在 application 类中初始化它。

下面是我的模块,dagger2 的组件,有助于解决依赖关系。

NetComponent.java

@Singleton
@Component(modules = {AppModule.class, NetModule.class})
public interface NetComponent {
void inject(AuthenticationActivity authenticationActivity);

void inject(PaymentActivity paymentActivity);
}

AppModule.java

@Module
public class AppModule {

private Application application;

public AppModule(Application application) {
this.application = application;
}

@Provides
@Singleton
Application providesApplication() {
return application;
}
}

NetModule.java

@Module
public class NetModule {

@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}

@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}

@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}

@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.newBuilder()
//.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.cache(cache)
.build();
return okHttpClient;
}

@Provides
@Singleton
@Named("authRetrofit")
Retrofit provideAuthRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(PAYMENT_SERVICE)
.client(okHttpClient)
.build();
return retrofit;
}

@Provides
@Singleton
@Named("paymentRetrofit")
Retrofit providePaymentRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(LOGIN_SERVICE)
.client(okHttpClient)
.build();
return retrofit;
}


}

AppApplication.java

public class AppApplication extends Application {

private NetComponent mNetComponent;

@Override
public void onCreate() {
super.onCreate();

mNetComponent = DaggerNetComponent.builder()
.appModule(new AppModule(this))
.build();
}

public NetComponent getmNetComponent() {
return mNetComponent;
}

}

Validator.java

@Module
public class Validator {

@Provides
com.mobsandgeeks.saripaar.Validator providesValidator(Application application) {
return new com.mobsandgeeks.saripaar.Validator(application);
}
}

我想将 Activity 实例传递给我正在使用它的 Validator 的构造函数。

假设我想在 MainActivity.java 中注入(inject) Validator ,那么构造函数应该有 MainActivity 实例。

我应该采取什么方法呢?我是否应该为此在 activity 中初始化 dagger 依赖项?我是否需要为此创建一个新组件?

最佳答案

您可以简单地为 ValidatorModule 创建构造函数:

@Module
public class Validator {

private final Activity activity;

public Validator(Activity activity) {
this.activity = activity;
}

@Provides
com.mobsandgeeks.saripaar.Validator providesValidator() {
return new com.mobsandgeeks.saripaar.Validator(activity);
}
}

请告诉我这是否是您正在寻找的内容

关于java - 如何将 Activity 实例发送到 dagger2 构造函数中的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43868078/

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