EventBus
EventBus is publish/subscribe event bus optimized for Android.
EventBus...
- simplifies the communication between components
- decouples event senders and receivers
- performs well with Activities, Fragments, and background threads
- avoids complex and error-prone dependencies and life cycle issues
- makes your code simpler
- is fast
- is tiny (<50k jar)
- is proven in practice by apps with 100,000,000+ installs
- has advanced features like delivery threads, subscriber priorities, etc.
EventBus in 3 steps
- Define events:
public class MessageEvent { /* Additional fields if needed */ }
- Prepare subscribers:
eventBus.register(this);
public void onEvent(AnyEventType event) {/* Do something */};
- Post events:
eventBus.post(event);
Add EventBus to your project
EventBus is available on Maven Central. Please ensure that you are using the latest version by checking here
Gradle:
compile 'de.greenrobot:eventbus:2.4.0'
Maven:
<dependency>
<groupId>de.greenrobot</groupId>
<artifactId>eventbus</artifactId>
<version>2.4.0</version>
</dependency>
Or download EventBus from Maven Central
How-to, Developer Documentation
Details on EventBus and its API are available in the HOWTO document.
How does EventBus compare to other solutions, like Otto from Square? Check this comparison.
Additional Features and Notes
- NOT based on annotations: Querying annotations are slow on Android, especially before Android 4.0. Have a look at this Android bug report.
- Based on conventions: Event handling methods are called "onEvent".
- Performance optimized: It's probably the fastest event bus for Android.
- Convenience singleton: You can get a process wide event bus instance by calling EventBus.getDefault(). You can still call new EventBus() to create any number of local busses.
- Subscriber and event inheritance: Event handler methods may be defined in super classes, and events are posted to handlers of the event's super classes including any implemented interfaces. For example, subscriber may register to events of the type Object to receive all events posted on the event bus.
FAQ
Q: How is EventBus different to Android's BroadcastReceiver/Intent system?
A: Unlike Android's BroadcastReceiver/Intent system, EventBus uses standard Java classes as events and offers a more convenient API. EventBus is intended for a lot more uses cases where you wouldn't want to go through the hassle of setting up Intents, preparing Intent extras, implementing broadcast receivers, and extracting Intent extras again. Also, EventBus comes with a much lower overhead.
Q: How to do pull requests?
A: Ensure good code quality and consistent formatting. EventBus has a good test coverage: if you propose a new feature or fix a bug, please add a unit test.
Release History, License
EventBus binaries and source code can be used according to the Apache License, Version 2.0.
More Open Source by greenrobot
greenrobot-common is a set of utility classes and hash functions for Android & Java projects.
greenDAO is an ORM optimized for Android: it maps database tables to Java objects and uses code generation for optimal speed.
Follow us on Google+ to stay up to date.