I have a simple Dagger 2 test installation based on http://konmik.imtqy.com/snorkeling-with-dagger-2.html . It introduces a PreferenceLogger, which displays all the settings. In the introduced class, I can @Inject more classes.
public class MainActivity extends Activity { @Inject PreferencesLogger logger; @Inject MainPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { MyApplication.getComponent().inject(this); presenter.doStuff(); logger.log(this); } } public class PreferencesLogger { @Inject OkHttpClient client; @Inject public PreferencesLogger() {} public void log(Contect context) {
When I ran this, a logger is installed, and inside PreferencesLogger.log, OkHttpClient is correctly installed. Thus, this example works as expected. Now I am trying to create an MVP structure. There MainPresenter interface with implementation. In MainActivity, I set:
@Inject MainPresenter presenter;
so I can switch this MainPresenter to an alternative (debug or test) implementation. Of course, now I need a module to indicate which implementation I want to use.
public interface MainPresenter { void doStuff(); } public class MainPresenterImpl implements MainPresenter { @Inject OkHttpClient client; public MainPresenterImpl() {} @Override public void doStuff() {
Now a problem arises when OkHttpClient is no longer entered. Of course, I could change the module to accept the OkHttpClient parameter, but I don't think this is the suggested way to do this. Is there a reason MainPresenterImpl is not entering correctly?
java android dependency-injection mvp dagger-2
R. adang
source share