Max
Max

Reputation: 2524

Set a custom listener to listen throughout the whole application

I was wondering if it's possible to have 1 listener listen for a specific event throughout the whole application.

At this moment I have created my own listener which is listening for an event to happen, however it should still listen to this event when I switch to another activity.

Upvotes: 4

Views: 195

Answers (1)

Bogdan Ustyak
Bogdan Ustyak

Reputation: 5837

Use http://square.github.io/otto/ for event listening.

This is an example of usage:

Bus bus = new Bus();

bus.post(new AnswerAvailableEvent(42));

@Subscribe public void answerAvailable(AnswerAvailableEvent event) {
    // TODO: React to the event somehow!
}

bus.register(this); // In order to receive events, a class instance needs to register with the bus.

Upvotes: 3

Related Questions