Event Bus in Android

Burak Taşcı
3 min readJun 17, 2023

--

What is an Event Bus?

An event bus is a communication mechanism used in software architecture to facilitate the exchange of events between different components or services within a system. It provides a way for components to publish and subscribe to events, enabling loose coupling and asynchronous communication.

The event bus acts as a central hub or message broker, allowing components to communicate without having direct knowledge of each other. Instead of components directly calling each other’s methods or functions, they publish events to the event bus, and other components interested in those events subscribe to them. This decouples the sender and receiver, making the system more modular, scalable, and flexible.

Event Bus Usages Purposes in Android

In Android applications, accessing and manipulating data or UI elements across different parts of the application can be a common requirement. However, directly communicating with the target components can introduce tight coupling and dependencies, making the codebase less maintainable and flexible. This is where an event bus implementation comes into play.

By using an event bus in an Android application, developers can achieve a flexible environment for accessing or manipulating data and UI elements without requiring direct references to the target components.

When a component wants to communicate or trigger an action in another component, it publishes an event to the event bus instead of directly referencing the target component. The event can carry any relevant data or instructions. Other components that are interested in that event can subscribe to it, indicating their desire to be notified when such an event occurs.

Implementation of Event Bus in Android Over Years

Custom Implementations (Early Years):

  • Developers implemented their custom event bus systems tailored to their application’s needs, using interfaces, callbacks, or broadcasting mechanisms provided by the Android framework.

Otto Event Bus (2012):

  • Square Inc. introduced Otto, an event bus library designed for Android applications.
  • Otto simplified event-driven programming by providing a lightweight publish/subscribe mechanism with annotations and automatic event delivery.

EventBus by greenrobot (2013):

  • greenrobot’s EventBus library offered a simple and high-performance event bus implementation for Android.
  • It became widely adopted due to its ease of use, flexibility, and support for various event delivery modes.
  • EventBus provided a hierarchical event bus structure and allowed developers to post events and subscribe to them using annotations.

RxBus (2015):

  • RxBus is an event bus implementation based on Reactive Extensions (Rx) for Android.
  • RxBus leverages the power of RxJava and RxAndroid to provide a reactive approach to event-driven programming.
  • It utilizes observables and subscribers, allowing components to subscribe to events and receive them in a reactive manner.

Android Jetpack LiveData (2017):

  • LiveData, part of Android Jetpack, acted as a lifecycle-aware data holder component with event bus-like behavior.
  • LiveData simplified event-driven communication by automatically handling lifecycle events and ensuring UI updates on the main thread.

Kotlin Coroutines with SharedFlow(Present):

  • Kotlin Coroutines are commonly used for event-driven programming in modern Android applications.
  • SharedFlow, a hot stream in Kotlin Coroutines, is often utilized to implement an event bus-like mechanism.
  • With SharedFlow, events can be emitted by a publisher and received by multiple subscribers in a reactive manner.

Advantages and Disadvantages of Event Bus Implementation in Android

Advantages

  • Loose coupling between components, promoting modularity and maintainability.
  • Decoupled communication through events, reducing direct dependencies.
  • Simplified asynchronous communication, improving responsiveness and preventing UI blocking.
  • Event-driven architecture, making the codebase more readable and organized.
  • Cross-component communication, enabling seamless communication between different parts of the application.

Disadvantages

  • Increased complexity in the application architecture.
  • Potential performance overhead due to event dispatching and handling.
  • Management of event types and subscriptions can become challenging in large projects.
  • Possible overuse or misuse, leading to a convoluted architecture.
  • Debugging and troubleshooting can be more challenging, requiring proper logging and error handling mechanisms.

In Conclusion

An event bus in Android is a valuable communication mechanism that facilitates the exchange of events between components, promoting loose coupling and modularity. However, it’s important to consider the potential drawbacks, such as increased complexity, performance overhead, and the need for proper event type management. Nonetheless, by leveraging the advantages and addressing these limitations, an event bus implementation can greatly enhance the architecture and communication within an Android application, resulting in more scalable and maintainable applications.

For implementation: https://github.com/Burak-Tasci/CoroutinesBus

Resources & References:

[1]:https://www.techyourchance.com/event-bus/

[2]: https://guides.codepath.com/android/communicating-with-an-event-bus

[3]:https://www.linkedin.com/pulse/android-eventbus-cons-yagoub-grine/

--

--