Skip to main content

How to receive events with EventBus in Kotlin Android

How to receive events with EventBus in Kotlin Android.

EventBus is a popular open-source library for Android that simplifies communication between components in an application using an event-driven architecture. It allows decoupling of components by providing a publish-subscribe mechanism, where components can publish events and other components can subscribe to those events.

In this tutorial, we will learn how to receive events using EventBus in a Kotlin Android application. We will go through the step-by-step process with code examples to illustrate each step.

Step 1: Add EventBus Dependency

First, we need to add the EventBus dependency to our project. Open the build.gradle file for your app module and add the following line to the dependencies block:

implementation 'org.greenrobot:eventbus:3.2.0'

After adding the dependency, sync your project to download and add the library to your project.

Step 2: Define Event Classes

To use EventBus, we need to define event classes that represent the events we want to publish and subscribe to. An event class is a plain Kotlin class that contains the necessary data for an event.

For example, let's define a simple event class called MessageEvent that represents a message being sent:

class MessageEvent(val message: String)

Step 3: Register and Unregister Event Subscriber

To receive events, we need to register our event subscriber with EventBus. Typically, this is done in the onCreate method of an activity or a fragment.

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
EventBus.getDefault().register(this)
}

Don't forget to unregister the event subscriber when it's no longer needed. This is usually done in the onDestroy method.

override fun onDestroy() {
super.onDestroy()
EventBus.getDefault().unregister(this)
}

Step 4: Subscribe to Events

To subscribe to events, we need to define a function in our event subscriber that will be called when an event is published. This function should have a single parameter representing the event.

For example, let's define a function called onMessageEvent that will be called when a MessageEvent is published:

@Subscribe
fun onMessageEvent(event: MessageEvent) {
// Handle the event
val message = event.message
// Do something with the message
}

Step 5: Post Events

To publish an event, we need to create an instance of the event class and post it to EventBus. Any subscriber that has registered for that event will receive it.

For example, let's post a MessageEvent with a message "Hello, EventBus!":

val event = MessageEvent("Hello, EventBus!")
EventBus.getDefault().post(event)

Step 6: Complete Example

Here's a complete example that demonstrates the usage of EventBus:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
EventBus.getDefault().register(this)
}

override fun onDestroy() {
super.onDestroy()
EventBus.getDefault().unregister(this)
}

@Subscribe
fun onMessageEvent(event: MessageEvent) {
val message = event.message
// Handle the event
}

fun sendMessage() {
val event = MessageEvent("Hello, EventBus!")
EventBus.getDefault().post(event)
}
}

class MessageEvent(val message: String)

In this example, we register the MainActivity as an event subscriber, define a function onMessageEvent to handle MessageEvent, and post a MessageEvent with a message "Hello, EventBus!".

That's it! You've learned how to receive events using EventBus in Kotlin Android. EventBus simplifies communication between components in your application and helps maintain a decoupled architecture.