This is a simple demo using EventBus. EventBus is a publish/subscriber event bus optimized for Android and developed by Markus Junginger in Greenrobot.
-
Define events:
public class GlobalBus { private static EventBus eventBus; public static EventBus getEventBus() { if (eventBus == null) { eventBus = EventBus.getDefault(); } return eventBus; } }
-
Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:
@Subscribe(sticky = true) public void onMessageEvent(EventMessageList event) { /* Do something */ }
Register and unregister your subscriber. For example on Android, activities and fragments should usually register according to their life cycle:
@Override protected void onStart() { super.onStart(); GlobalBus.getEventBus().register(this); } @Override protected void onStop() { GlobalBus.getEventBus().unregister(this); super.onStop(); }
-
Post events:
GlobalBus.getEventBus().postSticky(new EventMessageList(messageList));
Read the full getting started guide.