Skip to content

How eventful the Event Bus is in Android

Published: at 5 min read

What is Event Bus

Concept of Event Bus

  1. Publishing Events: Components or modules can publish custom-defined events to the event bus when specific actions or events occur.

  2. Subscribing to Events: Other components or modules can subscribe to the event bus in specific types of events. They provide callback methods or handlers that should be invoked when those events are published.

  3. Decoupling: Event buses promote loose coupling between components which makes the system more modular, maintainable, and extensible.

  4. Asynchronous Communication: Event buses often support asynchronous communication, i.e events can be published and consumed on different threads or at different times without blocking the main execution thread.

  5. Event Propagation: The event bus is responsible for delivering events to the appropriate subscribers based on their subscriptions and manages the routing and delivery of events.

  6. Event Handling: Subscribers typically define event handlers or callback methods when specific event occur and perform actions, update state, or trigger further processing.

Event bus libraries available for Android development, such as:

Extra Pinch with Event Bus in Android

With the introduction of Android Architecture Components and ViewModel pattern, LiveData and ViewModel both are used to handle communication between components.

However, event buses are still a choice for certain use cases, especially when dealing with legacy code or when a more flexible event-driven architecture is preferred.

Usage of Event Bus in Android

We use EventBus because

USAGE ~

Step 1 : Add Event Bus dependency in app/build.gradle

implementation 'org.greenrobot:eventbus:3.2.0'

Step 2 : Create event classes that represent the events you want to send and receive. These classes should contain any relevant data that you want to pass with the events.

public class MessageEvent {
    private String message;

    public MessageEvent(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

Step 3 : Register and Unregister Subscribers

@Override
protected void onStart() {
    super.onStart();
    EventBus.getDefault().register(this); // Register as a subscriber
}

@Override
protected void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this); // Unregister when the component is no longer active
}

Step 4 : Subscribe to Events

@Subscribe
public void onMessageEvent(MessageEvent event) {
    // Handle the received message event
    String message = event.getMessage();
    // Update your UI or perform other actions
}

Step 5:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this); // Register as a subscriber
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this); // Unregister when the component is no longer active
    }

    @Subscribe
    public void onMessageEvent(MessageEvent event) {
        // This method will be called when a MessageEvent is posted on the event bus
        String message = event.getMessage();
        // Do something with the message
    }

    // ...
}

Step 6 : Post Events

EventBus.getDefault().post(new MessageEvent("Hello, Earth!"));

This line posts a MessageEvent to EventBus with the message “Hello, Earth!“.

Step 7 : Receive and Handle Events

Step 8: Handle Thread Considerations

@Subscribe(threadMode = ThreadMode.X)

One needs to be careful of thread synchronization when updating UI elements from background threads.

For example, please follow the link : https://github.com/greenrobot-team/greenrobot-examples/tree/master/eventbus-kotlin/app/src/main/java/com/example/eventbuskotlin

Drawbacks of Event Bus in Android

Summary

That’s a basic overview of how to use EventBus in Android. It simplifies communication between components by using a publish-subscribe mechanism. It is required to register and unregister the subscribers to prevent memory leaks and to follow best practices for event-driven architecture in the Android application.

This library itself stopped maintenance 1 or 2 years back, but in some projects developers use this

Share :
Written by:Parita Dey

Interested in Writing Blogs, showcase yourself ?

If you're passionate about technology and have insights to share, we'd love to hear from you! Fill out the form below to express your interest in writing technical blogs for us.

If you notice any issues in this blog post or have suggestions, please contact the author directly or send an email to hi@asdevs.dev.