Event bus for android
-
Upload
ting-cheng -
Category
Engineering
-
view
216 -
download
4
Transcript of Event bus for android
EventBus for AndroidTing Cheng
Intent, Handler,
Broadcast
Status Quo in Android
Fragment Fragment
Activity
Service / Helper
Thread
Activity
Issues !
• Define interfaces
• Callbacks for async
• Listener management
• Propagation through all layers
EventBus
Fragment Fragment
Activity
Service / Helper
Thread
Activity
Event
Bus
Three things
• Event
• Subscriber
• Publisher
EventBus in 4 Steps
1. Define an event
public class MyEvent {}
2. Register subscriber
EventBus.getDefault().register(this);
3. Post an event
EventBus.getDefault().post(event);
4. Receive the event
public void onEvent(MyEvent event);
Publish / Subscribe
PublisherEvent
Bus
Subscriber
Event
post()
EventonEvent()
Subscriber
Event
onEvent()
• EventBus instances
• It anywhere in your code
• Event handler methods
• Naming convention: onEvent
• public visibility
• No return value (void)
• Single parameter for the event to receive
Event Type is a Filter
PublisherEvent
Bus
Subscriber
Event
post
(MyEvent)
EventonEvent(MyEvent)
Subscriber
onEvent(OtherEvent)
Code: Sender
MyEvent myEvent = new MyEvent(anyData);
EventBus.getDefault().post(myEvent);
Code: ReceiveMyActivity extends Activity
// onCreate or onResume
EventBus.getDefault().register(this);
// onDestroy or onPause
EventBus.getDefault().unregister(this);
public onEvent(MyEvent event) { … }
Example:
Fragment to Fragment
ListFragment DetailFragment
Activity
DetailFragment
// Here‘s the action. How is it called?
public void loadDetails(Item item) {
…
}
ListFragment
interface Contract {
void onItemSelected(Item item);
}
// Propagate to Activity
((Contract)getActivity()).onItemSelected(item);
Activity
public class MyActivity implements ListFragment.Contract {
public void onItemSelected(Item item) {
DetailFragment detailFragment = (DetailFragment)
getFragmentManager().findFragmentBy…;
detailFragment.loadDetails(item);
}
}
EventBus Example
Event class
public class ItemSelectedEvent {
public final Item item;
public ItemSelectedEvent(Item item) {
this.item = item;
}
}
Post / Receive
// In ListFragment
EventBus.getDefault().post(new ItemSelectedEvent(item));
// In DetailFragment (extends EventBusFragment)
public void onEvent(ItemSelectedEvent event) {
…
}
Thread Modes
• PostThread: Direct call in same thread
• MainThread: UI updates etc.
• BackgroundThread: „not the main thread“
• Async: always asynchronous to posting
• Used in the method name: onEventXXX
• No additional code required
Tips
Sticky Event• Events can be posted using sticky mode
postSticky(event); // Instead of post(…)
• The last sticky event is kept in memory
• Registration with getting sticky events
registerSticky(subscriber);
• Query for sticky event
getStickyEvent(Class<?> eventType)
• Remove sticky event (Class or Object)
removeStickyEvent(Class<?> eventType)
When to use EventBus• Medium/high distance of components:
• Potentially multiple dependencies
• Asynchronous logic
• When not to use EventBus
• Strictly internal matters:
• Use methods and interfaces instead
• Inter-process communication