Dagger2 different scope modules injection not work

2019-06-09 19:50发布

I am playing with Dagger2 framework at android platform. I have imported my code at GitHub . While injecting more than 2 different modules at My ScreenA class , all the dependencies are injecting properly. But, while I am passing param from Screen A to Screen B and consuming parsed param at Screen B inner class ,all the injected modules instance becomes null.

Screen A source codes :

@Layout(R.layout.screen_a) public class ScreenA extends Path implements ScreenComponentFactory<MainActivity.Component> {
@Override
public Object createComponent(MainActivity.Component parent) {
    return DaggerScreenA_Component.builder().component(parent).fingerPrintModule(new FingerPrintModule()).moduleDependencies(new ModuleDependencies()).build();
}

@dagger.Component(dependencies = MainActivity.Component.class,modules = {ModuleDependencies.class, FingerPrintModule.class})
@DaggerScope(Component.class)
public interface Component extends AppDependencies {

    void inject(ViewA view);

    SharedPreferences sharedPreferences();

    FingerprintManager fingerprintManager();

    KeyguardManager keyguardManager();
}

@DaggerScope(Component.class)
public static class Presenter extends ViewPresenter<ViewA> {
    @Inject
    RestClient restClient;

    @Inject
    ResponseCache responseCache;

    @Inject
    SharedPreferences sharedPreferences;

    @Inject
    FingerprintManager fingerprintManager;

    @Inject
    KeyguardManager keyguardManager;

    Context context;

    public Callback<WeatherResponse> configServiceCallback = new Callback<WeatherResponse>() {
        @Override
        public void onResponse(Call<WeatherResponse> call, Response<WeatherResponse> response) {
            Log.d("ScreenA","Response data -->"+response.body().toString());
            responseCache.setWeatherResponse(response.body());
            Flow.get(context).setHistory(History.single(new ScreenB("object")), Flow.Direction.FORWARD);
        }

        @Override
        public void onFailure(Call<WeatherResponse> call, Throwable t) {

        }
    };


    private String errorMessage = "";

    @Inject
    Presenter() {
    }

    @Override
    protected void onLoad(Bundle savedInstanceState) {
        super.onLoad(savedInstanceState);
        context = getView().getContext();
        Log.e("Screen A","shared preferences-->"+sharedPreferences.getBoolean("HARI",false));
        Log.e("Screen A","fingerprintManager-->"+fingerprintManager);

            restClient.getService(WeatherService.class).getConfiguration().enqueue(configServiceCallback);


    }


}

}

Screen B source code :

@Layout(R.layout.screen_b) public class ScreenB extends Path implements ScreenComponentFactory<MainActivity.Component> {

private static String value = "";

public ScreenB(String value) {
    this.value = value;
}

@Override
public Object createComponent(MainActivity.Component parent) {
    return DaggerScreenB_Component.builder().component(parent).fingerPrintModule(new FingerPrintModule()).module(new Module()).build();
}

@dagger.Component(dependencies = MainActivity.Component.class,modules = {FingerPrintModule.class,Module.class})
@DaggerScope(Component.class)
public interface Component extends AppDependencies {

    void inject(ViewB view);

    FingerprintManager fingerprintManager();

    KeyguardManager keyguardManager();
}

@DaggerScope(Component.class)
public static class Presenter extends ViewPresenter<ViewB> {

    Context context;

    @Inject
    FingerprintManager fingerprintManager;

    private String errorMessage = "";

    @Inject
    ResponseCache responseCache;

    String value = "";

    @Inject
    Presenter(String value) {
        this.value= value;
    }

    @Override
    protected void onLoad(Bundle savedInstanceState) {
        super.onLoad(savedInstanceState);
        context = getView().getContext();
        Log.d("ScreenB","Finger print manager-->"+fingerprintManager);
        Log.d("ScreenB","value-->"+value);
        Log.d("ScreenB","Response cache -->"+responseCache);

    }


}

@dagger.Module
public static class Module {

    @Provides
    @DaggerScope(Component.class)
    public String provideValue() {
        return value;
    }

}

}

For your reference I am providing logger also below,

For Screen A:

shared preferences-->false 
fingerprintManager->android.hardware.fingerprint.FingerprintManager@d504f00

For Screen B:

Finger print manager-->null
value-->object
Response cache -->null

Expected : Screen B Finger print manger should not be null & Response cache also should not be null .( If I removed ScreenB.Module from ScreenB.Component Finger print manger and Response cache is not null. )

If any one faced this scenario, Please share your thoughts to fix this issue.

0条回答
登录 后发表回答