3

I've been playing with Otto for the last few days and it's just amazing. However, I've run into a problem, I"m trying to communicate from a Fragment to the Activity. To keep things simple, I have one Activity which hold the Fragment, and in the Fragment I have a Button which just simply posts an event.

@Inject Bus bus;
...
...
bus.post(new ReadStatusEvent("23"));

In the Activity I have a Subscribe method.

@Subscribe
public void onReadStatusEvent(ReadStatusEvent event){
    Timber.i("sub:"+event.getReadStatusID());
}

Here is the event

public class ReadStatusEvent {
  private final String readStatusID;

  public ReadStatusEvent(String readStatusID) { this.readStatusID = readStatusID; }

  public String getReadStatusID() { return readStatusID; }
}

I'm using Dagger, so my Otto Bus is a singleton which is Injected by the SimpleModule file.

@Provides @Singleton
Bus provideBus() {
    return new Bus(ThreadEnforcer.ANY);
}

I've been going at it for the whole day but for some reason the Subscribe method is never called.

Edit: I've just tried using the Otto plugin for Android Studio and whenever I click the @Subscribe or bus.post(..), it keeps searching but doesn't find anything(No usage found so far). I guess I'm doing something terribly wrong somewhere.

Raza
  • 1,192
  • 4
  • 12
  • 23

2 Answers2

11

Your hint concerning the Otto Plugin made me think about your imports.

Are you sure you are using "com.squareup.otto.Subscribe" and not "com.google.common.eventbus.Subscribe" or any other package?

Sebastian Engel
  • 3,500
  • 32
  • 30
  • FML! You are right I never noticed the import, just changed it and it's working flawlessly now. THANKS A LOT! – Raza Mar 14 '14 at 17:48
0

In order to receive events, a class instance needs to register with the bus. Are you registering the objects holding your subscribers/producers?

bus.register(this); // Example
Tadej
  • 2,901
  • 1
  • 17
  • 23