There is a simple / obvious workaround to achieve the same, depending on how the rest of your code looks.
Without using field injection to initialize the mView field of the base presenter, you can simply pass it to the constructor and provide it with a module, for example:
public abstract class Presenter<T extends BaseView> { private final T mView; public Presenter(T view) { mView = view; } public void showLoadingIndicator() { mView.showLoading(); } }
provided that it is used as follows:
public class MainPresenter extends Presenter<MainActivity> { public MainPresenter(MainActivity view) { super(view); } }
and the module creates the presenter and gives him the view:
@Module(injects = MainActivity.class) public class MainModule { private final MainActivity mMainActivity; public MainModule(MainActivity mainActivity) { mMainActivity = mainActivity; } @Provides MainPresenter mainPresenter() { return new MainPresenter(mMainActivity); } }
I prefer this anyway, because I prefer constructor injection over field injection (except for objects not created by the dagger, such as activity level or fragment, of course, where we cannot escape @Inject ).
here is the code
source share