android - how can I inject a dependency in a method? -


i'm beginner dependency injection.. dagger 2. i'm trying figure out if/how can this:

@inject public void somemethodname(int someinteger, someobject dependency){  // dependency.  } 

or need put dependency in class var? appreciated. in case variable someinteger not dependency, being added caller... matter?

can call this:

this.somemethodname(5);  

android studio not above calling method (i'm assuming because i'm doing wrong)

  1. you need create component annotated @component.
  2. the component accepts module provides dependencies.
  3. every component's name create starts dagger prefix, e.g. mycomponent.

let's @ following example:

    @singleton     @component(modules = demoapplicationmodule.class)     public interface applicationcomponent {         void inject(demoapplication application);     } 

we created applicationcomponent single injection method. we're saying want inject dependencies in demoapplication.

moreover, in @component annotations specify module provision methods.

this our module looks like:

@module public class demoapplicationmodule {    private final application application;     public demoapplicationmodule(application application) {        this.application = application;     }    @provides @singleton someintegerhandler provideintegerhandler() {        return new mysomeintegerhandlerimpl();   }  } 

what we're saying creating demoapplicationmodule module can provide desired dependencies in injection place specified our component.

 public class demoapplication extends application {    private applicationcomponent applicationcomponent;     @inject someintegerhandler handler;     @override public void oncreate() {      super.oncreate();      applicationcomponent = daggerapplicationcomponent.builder()          .demoapplicationmodule(new demoapplicationmodule(this))          .build();      applicationcomponent.inject(this);      handler.somemethodname(5);    }   } 

see documentation kind of dependencies can obtain. additionally obtaining raw instance can obtain provider, factory or lazy instance. http://google.github.io/dagger/api/latest/dagger/component.html

you can create scoped dependencis, lifecycles of depend on lifecycle of injection places, activities or fragments.

hope gave basic notion of dagger is.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -