I asked this question before: Dagger 2 Third of Injection Android , to which I received an answer. After that, I found out something else, and I believe that I am not well defined to explain my problem. Now this question has been cluttered, so I am posting this new new question.
I am trying to implement a third-party library, I need to specify a specific specification for the Otto bus using Dagger 2. This bus needs to be introduced into several services and activation because I use an event-driven architecture.
So, as mentioned earlier, the module component has been created. And, as answere says, I have to create a graph to be able to @ Inject bus into classes.
I need to inject each class into the components that I have, but I wonder how I can create this component family component. The answer to this says:
@Component(modules = EventBusModule.class)
@Singleton
public interface EventBus {
Bus bus();
void inject(WearService wearService);
}
public class MyApplication extends Application {
private EventBus mEventBusComponent;
@Override
public void onCreate() {
super.onCreate();
mEventBusComponent = Dagger_EventBus.create();
}
public void inject(WearService wearService) {
mEventBusComponent.inject(wearService);
}
}
public class WearService extends WearableListenerService {
public int onStartCommand(Intent intent, int flags, int startId) {
((MyApplication) getApplicationContext()).inject(this);
}
}
Iv tried to do this using Generics, but kept getting this error:
Error:(13, 14) error: Type parameters must be bounded for members injection.
@Component(modules = CommunicationModule.class)
@Singleton
public interface AppComponent {
<T> void inject(Class<T> module);
Bus bus();
}
public <T> void inject(Class<T> module) {
mAppComponent.inject(module);
}
source
share