Dagger 2 - two provides method that provide same interface
Date : March 29 2020, 07:55 AM
this will help I recently post the answer to a question like this in this post : Dagger 2 : error while getting a multiple instances of same object with @Named@Module
public class ApplicationModule {
private Shape rec;
private Shape circle;
public ApplicationModule() {
rec = new Rectangle();
circle= new Circle ();
}
@Provides
@Named("rect")
public Shape provideRectangle() {
return rec ;
}
@Provides
@Named("circle")
public Shape provideCircle() {
return circle;
}
@Inject
@Named("rect")
Shape objRect;
@field:[Inject Named("rect")]
lateinit var objRect: Shape
|
Dagger 2: Provide same instance between multiple Component with same Scope on different library modules
Tag : java , By : Zinovate
Date : March 29 2020, 07:55 AM
Any of those help Remove the injection sites from your CoreComponent - it now has the sole function of exposing the binding for CoreRepository to its dependent components: @Singleton
@Component(modules = {CoreModule.class})
public interface CoreComponent {
CoreRepository coreRepository();
}
public class MyApplication extends Application {
private final CoreComponent coreComponent;
@Override
public void onCreate() {
super.onCreate();
coreComponent = DaggerCoreComponent
.coreModule(new CoreModule())
.build();
}
public static CoreComponent getCoreComponent(Context context) {
return ((MyApplication) context.getApplicationContext()).coreComponent;
}
}
@Scope
@Retention(RetentionPolicy.RUNTIME) public @interface PerActivity {}
@PerActivity
@Component(dependencies = {CoreComponent.class})
public interface ActivityComponent {
void inject(FooActivity activity);
void inject(BarActivity activity);
}
public class FooActivity extends AppCompatActivity {
@Inject
public CoreRepository repo;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CoreComponent coreComponent = MyApplication.getCoreComponent(this);
DaggerActivityComponent.builder()
.coreComponent(coreComponent)
.build()
.inject(this);
}
}
}
|
Can dagger provide different implementations of the interface, depending on requested type?
Date : March 29 2020, 07:55 AM
it should still fix some issue This really depends on what you are trying to accomplish. You have 2 basic setups, whether you want to access your different implementations at once, or just want to supply one. Access them at the same time / within the same scope @Binds
@Qualifier("twitter")
Api provideApi(TwitterApi api);
@Inject @Qualifier("twitter") Api api;
@Component
interface ApiComponent {
Api api();
@Component.Builder
interface Builder {
@BindsInstance Builder api(Api api);
ApiComponent build();
}
}
|
(Dagger 2) Provide same instance for different types in MVP
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You can use constructor injection along with the @Binds annotation in your module to achieve this. Simply add a constructor and annotate it with @Inject in MyView. You can then mark MyView as @Singleton so that the same instance is used everywhere(assuming the component is also scoped with @Singleton). @Singleton
class MyView extends MyAbstractView implements MyViewContract {
@Inject
public MyView() {}
}
@Module
abstract class MyModule {
@Binds
abstract MyInterface provideMyInterface(MyView myView);
@Binds
abstract MyViewContract provideMyViewContract(MyView myView);
}
|
Dagger can't inject a parameter of type interface on a ViewModel's constructor using Dagger Android
Date : March 29 2020, 07:55 AM
|