0

I want to send data from the activity to my fragment using otto event bus

From my activity:

@Produce
public EventAvailableEvent produceEvent() {
    return new EventAvailableEvent(mEvent);
}

To fragment:

@Subscribe
public void onProvideEvent(EventAvailableEvent event) {
    mEvent = event.getEvent();
}

I'm using dagger 2 to inject bus

@Inject Bus mBus;

private void injectDepedencies() {
    App.from(getActivity()).getComponent().plus(new MyModule(mEvent));
}

My module is dependant to the event returned by event bus.

Right now, what I do is inject first the main component, register bus, then inject subcomponent

AppComponent appComponent = App.from(getActivity()).getComponent();
appComponent.inject(this)
mBus.register(this)
SubComponent subComponent = appComponent.plus(new MyModule(mEvent));
subComponent.inject(this)

I'm looking for a better way to this, thanks

Miguel Fermin
  • 380
  • 1
  • 3
  • 13

1 Answers1

0

Since I guess your SubComponent is a subcomponent of AppComponent it inherits all of it's provided items. Described here

That relationship allows the subcomponent implementation to inherit the entire binding graph from its parent when it is declared.

This means, you can just kick out the first 2 lines and that you just have to create your SubComponent.

App.from(getActivity()).getComponent().plus(new MyModule(mEvent)).inject(this);
mBus.register(this);

Would actually be enough.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134