Tuesday, April 19, 2016

Event bus in android

Before learning how to implement event bus on Android, lets try to understand what's event bus and a scenario where it can be helpful.

There are two similar concepts that we need to understand here
Observer pattern
This is a pattern of development in which your class or primary object (known as the Observable) notifies other interested classes or objects (known as Observers) with relevant information (events). Your class will actually have a list of interested class that it needs to inform.
pub-sub pattern
Objective of this pattern is exactly same as the above pattern BUT, the focus here is on broadcasting the event. It is up to the Observer to receive the information
One of the most important advantage of this anonymity is it is easy to "Decouple" the Observable and the Observers. In this case Observable is the Publisher and Observers are the Subscribers. Hence the name pub-sub.
To achieve this decoupling, we will be using a middleman who handles the communication between pub and sub. Square's Otto and Green Robot’s EventBus

Lets think of a scenario, where event bus can be useful. On a login activity, when a user clicks on login button, most of the time a call will be made to REST server to check the user's authentication. During this background activity, if you are using a third party HTTP client like retrofit, we seldom use AsyncTask class. Retrofit would make all the calls in background in an "async" way. Depending on the result from the REST server we have to take further actions. when all these are happening in background, obviously we need to show user a dialogue that says that we are processing the request. In this situation we can have a "subscriber" who would receive an "event" after the REST call is a success/failure.

I will be using Green Robot’s EventBus here, as it is very simple and has few additional feature than Otto. https://github.com/greenrobot/EventBus/blob/master/COMPARISON.md can be checked for more information.
You can implement Green Robot’s EventBus in 3 easy steps.

  1. Create a POJO with all the information that you need to send form a "publisher" to "subscriber". This is the event that will be broadcasted

     public class MessageEvent {   
        public final String message;   
        public MessageEvent(String message) {   
          this.message = message;   
        }   
     }   
    

  2. Define Subscribers. This is as simple as adding a annotation for the method. Add the annotation @Subscribe for a method which will accept the event defined in the above step as a parameter.

     public class MessageEvent {   
        public final String message;   
        public MessageEvent(String message) {   
          this.message = message;   
        }   
     }   
    

  3. Post the event

     public class MessageEvent {   
        public final String message;   
        public MessageEvent(String message) {   
          this.message = message;   
        }   
     }   
    


Also, do not forget to register/unregister the subscribers on activity start and stop
 public class MessageEvent {   
    public final String message;   
    public MessageEvent(String message) {   
      this.message = message;   
    }   
 }