Search code examples
androidandroid-dialogfragment

Using Otto for bidirectional communication with a dialog


I'm trying to pass an event from a fragment to a dialog using otto. Unfortunately by the time the dialog is created the event has already fired and the @Subscribe method in my dialog and the data that was being sent gets passed over.

The data I'm trying to pass is an Object and an index (Best case) or a String (worst case) from a listview/ArrayList.

I know they have an @Produce method; but I don't understand how a method that takes no arguments helps in anyway or how I can get it to work.

Here's an example they provide

@Produce public AnswerAvailableEvent produceAnswer() {
    // Assuming 'lastAnswer' exists.
    return new AnswerAvailableEvent(this.lastAnswer);
}

For my newly created dialog this.lastAnswer; won't exist, it can't - that's what I'm passing in.So how do I get around this?

Side note: I think the event bus, in it's current form is a bit of overkill since it's only communicating with my dialog. Later on I hope to use the event bus in more dialogfrag/fragment communication.


Solution

  • I've also asked same question for myself, when I was investigating Otto. And decided to read their site(http://square.github.io/otto/) more thoughtfully. Here is their description under @Produce annotation:

    ... Producers, like subscribers, must also be registered... When registering, the producer method will be called once for each subscriber previously registered for the same type. The producer method will also be called once for each new method that subscribes to an event of the same type.

    You may only have one producer per event type registered at a time on a bus.

    So I think in time you register your producer, "lastAnswer" should be initialized. And no parameters needed because you don't suppose to call your producer-method by yourself. I hope it will help.