java - How to provide objects of the same type? Dagger2 -


i new @ dagger2 , tried build such sample understood how work.

there sample code :

mainactivity

public class mainactivity extends appcompatactivity {  @inject protected apiinterface apiinterface;  @inject protected integer valueint;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      app.getcomponent().inject(this); }  public void testbutton(view view) {     if (apiinterface == null || valueint == null) {         log.e("tag", "apiinterface == null");     } else {         log.e("tag", "apiinterface != null : " + apiinterface.value +  " : " + valueint);     }  }  } 

component

@singleton @component(modules = {modelmodule.class, anothermodule.class}) interface appcomponent {  void inject(mainactivity mainactivity); } 

module

@module class modelmodule {  @provides int provideint() {     return 1; }  @provides apiinterface provideapiinterface(int i) {     return apimodule.getapiinterface(i); } } 

module

@module class anothermodule { @provides integer getint(){     return 3; } } 

as can see in sample in mainactivity inject integer

@inject protected integer valueint; 

and want use int provide value argument method provideapiinterface(int i).

and such error

error:(11, 10) error: java.lang.integer bound multiple times: @provides int com.krokosha.aleksey.daggertwo.modelmodule.provideint() @provides integer com.krokosha.aleksey.daggertwo.anothermodule.getint() 

what doing wrong?

how supposed provide argument in proper way avoid such error?

you need use qualifier annotations

@module class modelmodule {      @provides      @named("firstint")     int provideint() {         return 1;     } }  @module class anothermodule {      @provides      @named("secondint")     int provideint() {         return 1;     } } 

and use qualifiers when injecting dependecies

@inject protected apiinterface apiinterface;  @inject  @named("firstint") //or whatever need protected int valueint; 

hope helps! check official docs - http://google.github.io/dagger/


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -