[Android]Useful Library — Otto(Event Bus)

Otto is event bus library mady by Square Inc.



For example,

Here are A,B,C Activity.
This Activity has 3 screen view and use same data.
If data changed, A,B,C Activity must change their view.
If data changed in D Activity, this activity have to notify to A,B,C activity and deliver data to them.
Some people use static method and this static method change 3 Activity’s view.

When you use static method, your variable must be static variable.

But some variable can not be static(ex. Activity, Context) and that is so complex method

This is Event Bus’s architecture.

1. Register your event listener(Subscriber)
2. make your subscribe method(Subscriber)
3. publish your custom event(Publisher)
4. change data and adjust view(Subscriber)
5. unregister your event(Subscriber)

Otto — Event Bus


compile ‘com.squareup:otto:1.3.8’
  1. Make your custom BusProvider (Singleton)
public final class BusProvider {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
return BUS;
private BusProvider() {
// No instances.

2. Register listener in your Activity/Fragment

protected void onCreate(Bundle arg0) {
// Register ourselves so that we can provide the initial value.
protected void onDestroy() {
// Always unregister when an object no longer should be on the bus.

unregister timing is depend on situation.
you can unregister when finish load and also can unregister onDestory() method.

3. Make Subscribe method

public void FinishLoad(PushEvent mPushEvent) {

when you publish PushEvent class, this method will call

4. Publish your Event

BusProvider.getInstance().post(new PushEvent(results));

Make your PushEvent and post your CustomEvent

This is PushEvent.

public class PushEvent
// the pushlist object being sent using the bus
private List<Push> pushlist;

public PushEvent(List<Push> pushlist)
this.pushlist = pushlist;
* @return the pushlist
public List<Push> getList()
return pushlist;