android - How do I get this statement to work in Non Activity Classes? -


i need statement activate dep. injection....

 ((app) getapplication()).inject(this);              //or in fragment   ((app) getactivity().getapplication()).inject(this); 

 

this statement works fine in activities , fragments , services, how statement work in non-activity/fragment/service based classes?

what dependency injection framework using? looks might dagger, in case can use constructor @inject annotation , have dependencies passed in through (where foo class , bar dependency):

private final bar mbar;  @inject public foo(bar bar) {     mbar = bar; } 

in module you'll need like:

@provides public foo providesfoo(foo foo) {     return foo; } 

if aren't using dagger (and if are), i'd recommend making static method in application class instance of application avoid casting it, , allow accessible anywhere within app (although i'd call in activities/fragments/services/etc.). use this:

private static app sinstance;  public static app getinstance() {     return sinstance; }  @overrride public void oncreate() {     super.oncreate();     sinstance = this; } 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -