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

标签 java android dagger-2

我在我的应用程序中使用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/

相关文章:

android - 尝试在 Genymotion 上使用 libgdx 运行 Android 游戏

android - 将 TreeMap<List<>,List<>> 拆分为两个 ArrayList

android - 通过找到将以60fps和30fps运行的最佳设置来摆脱性能设置旋钮

java - Dagger 2 - 无法初始化

java - 在 For 循环中等待 Bukkit 任务完成

java - 具有捕获组问题的正则表达式量词

android - 如何使用 Dagger 2.10 Android Injector 提供与 @SessionScope 和 @ActivityScope 的依赖关系?

java - 如何使用 Dagger2 库

java - Gzip 解压后多加一个字节...为什么?

java - Java 中的 ArrayList 是否有其他方法可以使用 Stream() 来实现?