eventbus for android
DESCRIPTION
EventBus is an Android optimized publish/subscribe event bus. A typical use case for Android apps is gluing Activities, Fragments, and background threads together.TRANSCRIPT
![Page 1: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/1.jpg)
EventBusfor Android.droidcon NL23.11.2012Markus Junginger
© Copyright 2012 Markus Junginger.All rights reserved.
![Page 2: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/2.jpg)
Android Developerssince 2007.Based in Munich.We are looking for:– Cool Android projects– Android developers(see Jobs)
Follow us:@greenrobot_de
![Page 3: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/3.jpg)
Random Qoutes
„Really easy to use“
„exc
elle
nt h
elp“
“very clean way for c
ommunication”
„Excellent!““I re
moved a lot of b
oiler plate”
„very easy and stra
ightforw
ard“
![Page 4: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/4.jpg)
Status Quo in Android
Q: Who is talking to whom?
![Page 5: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/5.jpg)
Status Quo in Android
Fragment Fragment
Activity
Service / Helper
Thread
Activity
![Page 6: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/6.jpg)
Communication Issues?!
Tight coupling of components Inflexible, changes are expensive
Boiler plate code– Define interfaces– Callbacks for asynch. communication– Listener management– Propagation through all layers
![Page 7: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/7.jpg)
EventBus Communication
Fragment Fragment
Activity
Service / Helper
Thread
Activity
EventBus
![Page 8: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/8.jpg)
EventBus for Android
Event-based Publish/Subscribe Bus: central communication Inspired by Guava‘s EventBus (Google) Android-optimized Open Source, Apache 2 License
https://github.com/greenrobot/EventBus
![Page 9: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/9.jpg)
EventBus in 4 Steps
1. Define an event
2. Register subscriber
3. Post an event
4. Receive the event
![Page 10: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/10.jpg)
EventBus in 4 Steps: Code!
1. Define an eventpublic class MyEvent {}
2. Register subscriberEventBus.getDefault().register(this);
3. Post an eventEventBus.getDefault().post(event);
4. Receive the eventpublic void onEvent(MyEvent event);
![Page 11: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/11.jpg)
Publish / Subscribe
PublisherEventBus
Subscriber
Event
post()
EventonEvent()
Subscriber
Event
onEvent()
![Page 12: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/12.jpg)
EventBus Instances
EventBus instances Instances are indepent from each other For many apps, a single instance is fine „Convenience“ InstanceEventBus.getDefault()
Aquire it anywhere(!) in your code
![Page 13: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/13.jpg)
Event Handler Method: onEvent EventBus scans subscribers
– During (first) registration of subscriber– For event handler methods
Event handler methods– Naming convention: onEvent– public visibility– No return value (void)– Single parameter for the event to receive
![Page 14: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/14.jpg)
Type-based Event Routing
Event type: Java class of the event To receive an event, its type must match post(new MyEvent()); onEvent(MyEvent event) {…}
Type hierarchy of events– Implement logging of all events:
onEvent(Object event)
![Page 15: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/15.jpg)
Event Type is a Filter
PublisherEventBus
Subscriber
Event
post(MyEvent)
EventonEvent(MyEvent)
Subscriber
onEvent(OtherEvent)
![Page 16: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/16.jpg)
CODEIt‘s time to see some
![Page 17: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/17.jpg)
EventBus Code: Sender
MyEvent myEvent = new MyEvent(anyData);EventBus.getDefault().post(myEvent);
![Page 18: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/18.jpg)
EventBus Code: Receiver
MyActivity extends Activity // no interf.
// onCreate or onResumeEventBus.getDefault().register(this);
// onDestroy or onPauseEventBus.getDefault().unregister(this);
public onEvent(MyEvent event) { … }
![Page 19: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/19.jpg)
Example: Fragment to Fragment Tap on one fragment Another fragment reacts Typical list/details scenario Conventional setup:
Activity is managingFragments
Fragment Fragment
Activity
![Page 20: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/20.jpg)
Conventional: DetailFragment
// Here‘s the action. How is it called?public void loadDetails(Item item) { …}
![Page 21: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/21.jpg)
Conventional: ListFragment
interface Contract {void onItemSelected(Item item);
}
// Propagate to Activity((Contract)getActivity()).onItemSelected(item);
![Page 22: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/22.jpg)
Conventional: Activity
public class MyActivity implements ListFragment.Contract {
public void onItemSelected(Item item) { DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentBy…; detailFragment.loadDetails(item); }}
![Page 23: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/23.jpg)
EventBus: Event class
public class ItemSelectedEvent { public final Item item;
public ItemSelectedEvent(Item item) { this.item = item; }}
![Page 24: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/24.jpg)
EventBus: Post & Receive
// In ListFragmentEventBus.getDefault().post(new ItemSelectedEvent(item));
// In DetailFragment (extends EventBusFragment)public void onEvent(ItemSelectedEvent event) {…}
![Page 25: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/25.jpg)
THREADING SUPPORTEvent Delivery in other Threads
![Page 26: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/26.jpg)
Why do Threads matter?
Responsive UI, Android conventions– Main/UI thread must not block– UI updates in the main thread– Networking etc. in background threads
Threads and Events– Thread posting events– Thread handling events– Posting and handling thread may differ
![Page 27: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/27.jpg)
EventBus Threading Support
Event delivery using threads Event handler method decides
(handler knows best what it is doing) Naming conventions for onEvent methods To deliver in the main thread:
onEventMainThread(…) (All methods beginning with onEvent are
checked for typos)
![Page 28: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/28.jpg)
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
![Page 29: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/29.jpg)
Threading Example
Network thread posts resultpost(new UserLoginEvent(name));
Fragment reacts to event in main threadpublic void onEventMainThread(UserLoginEvent e) { textView.setText(e.getName());}
![Page 30: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/30.jpg)
SNEAK PREVIEWAsynchronous execution and error dialogs
![Page 31: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/31.jpg)
Upcoming: EventBus 2.1
EventBus 2.1 is not final yet;Still gathering experience and feedback
Everything pushed to master branch No relevant changes in the core (planned) Comes with some helpers
– AsyncExecutor– Error dialog
![Page 32: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/32.jpg)
EventBus 2.1: AsyncExecutor
A thread pool, but with failure handling RunnableEx interface
– Like Runnable to define code to run async.– May throw any execption
ThrowableFailureEvent– Posted when exception occurs– Throwable object is wrapped inside– Subclass to customize and filter for type
![Page 33: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/33.jpg)
AsyncExecutor: Calling Code
AsyncExecutor.create().execute( new RunnableEx { public void run throws LoginException { remote.login(); EventBus.getDefault().postSticky( new LoggedInEvent()); // No need to catch Exception } }}
![Page 34: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/34.jpg)
AsyncExecutor: Receiving Codepublic voidonEventMainThread(LoggedInEvent event) { // Change some UI}
![Page 35: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/35.jpg)
Comparison to AsyncTask
AsyncExecutor advantages– Don’t worry about configuration changes
(typically device rotation)– Failure handling– EventBus (other dependencies somewhere?)
AsyncTask advantage– Canceling task
![Page 36: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/36.jpg)
EventBus 2.1: Error Dialog
Operations may fail (networking, etc.) Some UI response should be shown
– Currently: error dialogs– Later: Possibly maybe someth. Crouton-ish
Multiple errors: don’t open multiple times Goal: Minimize required code
![Page 37: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/37.jpg)
How Error Dialogs work
Error handling “attaches” to Activities Displaying dialogs is triggered by events ThrowableFailureEvent or subclass
– AsyncExecutor sends those– Or post those events manually
![Page 38: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/38.jpg)
Error Dialogs: Code
// In onCreate in your ActivityErrorDialogManager.attachTo(this);
// Trigger the dialogbus.post(new ThrowableFailureEvent(ex));// Or let AsyncExecutor do it for you…
![Page 39: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/39.jpg)
Dialog Configuration
Which texts to display in the dialog? Simple option: Configure
– Supply default resources for title and text– Map exception types texts
Flexible option: extend ErrorDialogFragmentFactory
![Page 40: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/40.jpg)
Dialog Configuration: Code
// Initialize in your Application classErrorDialogConfig config = new ErrorDialogConfig(getResources(), R.string.error_generalTitle, R.string.error_general);config.addMapping(UnknownHostException.class, R.string.error_noInternetConnection);config.addMapping(IOException.class, R.string.error_generalServer);ErrorDialogManager.factory = new ErrorDialogFragmentFactory.Support(config);
![Page 41: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/41.jpg)
Comparison Android Broadcast Nice, but somewhat inconvenient BroadcastReceiver has to be extended Registration needed for every event type Extra work to filter for desired event types
(IntentFilter with e.g. ACTION String) Data is converted into Intent extras
![Page 42: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/42.jpg)
TIPPS AND SNIPPSTiny bits and some other stuff
![Page 43: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/43.jpg)
Sticky Events
Events can be posted using sticky modepostSticky(event); // Instead of post(…)
The last sticky event is kept in memory Registration with getting sticky eventsregisterSticky(subscriber);
Query for sticky eventgetStickyEvent(Class<?> eventType)
Remove sticky event (Class or Object)removeStickyEvent(Class<?> eventType)
![Page 44: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/44.jpg)
Events Class
Event classes are tiny; group them
public class Events { public static class InitThreadCompleteEvent { }
public static class LoginCompleteEvent { }}
![Page 45: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/45.jpg)
EventBusFragment/-Activity
Superclass depends on your setup Implement in your project
class EventBusFragment extends Fragment { public void onCreate(…) { super.onCreate(…); EventBus.getDefault().register(this); } public void onDestroy() { EventBus.getDefault().unregister(this); super.onDestroy(); }
![Page 46: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/46.jpg)
When to use EventBus
Medium/high distance of components:“Somewhere” else should happen sth.
Potentially multiple dependencies Asynchronous logic When not to use EventBus
– Strictly internal matters:Use methods and interfaces instead
– Inter-process communication
![Page 47: EventBus for Android](https://reader033.vdocuments.net/reader033/viewer/2022060108/554d1fd3b4c905ca208b4aca/html5/thumbnails/47.jpg)
License, Contact
This presentation is licensed under CreativeCommons ShareAlike/Attributionhttp://creativecommons.org/licenses/by-sa/3.0/
Contact greenrobot– http://greenrobot.de – http://twitter.com/greenrobot_de – Google+: http://bit.ly/greenrobotplus