如何在 dagger2 的构造函数中将 activity 实例发送到模块

How to send activity instance to module in a constructor in dagger2

我在我的应用程序中使用 dagger2。我已经创建了模块,组件,它在我的整个应用程序中,所以我在 application class.

中初始化它

下面是我的模块,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 中注入 Validator 那么构造函数应该有 MainActivity 个实例。

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

您可以简单地为您的 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);
    }
}

让我知道这是否是您要找的东西