java - how to use injection between MVP layers in android? -
i have been using using dagger 2 in project lately,
the problem when try build project, presenter in login activity injected below is null,
and when try build project
presenter cannot provided without @inject constructor or @provides- or @produces-annotated method...
i don't understand have done wrong??, please me this,
thanks in advance.
here login activity, presenter here null, shows that, i've not injected properly
@inject loginpresenter presenter; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); injecthelper.getrootcomponent().injectpresenter(this); presenter.setprogressbarvisiblity(view.invisible); }
this presenter module
@module public class presentermodule { private final loginactivity activity; public presentermodule(loginactivity activity) { this.activity = activity; } @provides @singleton public loginactivity providesview() { return activity; } } @provides @singleton public loginpresenter providespresenter() { return new loginpresenter(); } }
this inject helper class
public class injecthelper { private static rootcomponent srootcomponent; static { initmodules(); } private static void initmodules() { srootcomponent = getrootcomponentbuilder().build(); } public static daggerrootcomponent.builder getrootcomponentbuilder() { return daggerrootcomponent.builder(); } public static rootcomponent getrootcomponent() { if (srootcomponent == null) { initmodules(); } return srootcomponent; } }
this root component class
@singleton @component(modules = { presentermodule.class }) public interface rootcomponent { void injectloginview(loginpresenter loginpresenter); }
you need inform dagger views want use injection, in component. must change inject function code in component below:
void inject(loginactivity activity);
for showing dagger want, need use @dagger annotation not sending inject function in component file. did properly:
@inject loginpresenter presenter;
dagger search variable of type loginpresenter in module , finds proper provider method using type.
what put in component argument "inject" function tells dagger view going injection in (not want inject)
Comments
Post a Comment