Skip to main content

How to pass data with events using EventBus in Kotlin Android

How to pass data with events using EventBus in Kotlin Android.

Here's a step-by-step tutorial on how to pass data with events using EventBus in Kotlin Android.

Step 1: Add EventBus Dependency

To use EventBus in your Android project, you need to add the EventBus dependency to your app's build.gradle file.

implementation 'org.greenrobot:eventbus:3.2.0'

Step 2: Create Event Class

Create a Kotlin data class that represents the event and contains the data you want to pass. For example, let's create an event class called MessageEvent with a message property.

data class MessageEvent(val message: String)

Step 3: Register and Unregister Subscriber

In each component that wants to receive events, you need to register and unregister it with EventBus. For example, in an Activity, you can register it in the onStart() method and unregister it in the onStop() method.

class MainActivity : AppCompatActivity() {

override fun onStart() {
super.onStart()
EventBus.getDefault().register(this)
}

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

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

Step 4: Post Event

To pass data to the subscribers, you need to post an event using EventBus. You can do this from any component, such as an Activity or Fragment. For example, in an Activity, you can post an event in a button click listener.

class MainActivity : AppCompatActivity() {

// ...

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

Step 5: Subscribe to Event

To receive the event and the data it carries, you need to annotate a method in your subscriber component with @Subscribe. This method will be called when the event is posted.

class MainActivity : AppCompatActivity() {

// ...

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

Step 6: Event Priority (Optional)

If you have multiple subscribers for the same event, you can specify the priority of each subscriber. Subscribers with higher priority will receive the event before subscribers with lower priority. The default priority is 0.

@Subscribe(priority = 1)
fun onHighPriorityMessageEvent(event: MessageEvent) {
// Handle high priority event
}

@Subscribe(priority = 0)
fun onDefaultPriorityMessageEvent(event: MessageEvent) {
// Handle default priority event
}

@Subscribe(priority = -1)
fun onLowPriorityMessageEvent(event: MessageEvent) {
// Handle low priority event
}

That's it! You have now learned how to pass data with events using EventBus in Kotlin Android. EventBus simplifies the communication between components by using an event bus architecture, making it easy to decouple your code and improve maintainability.

Remember to always register and unregister your subscribers with EventBus to avoid memory leaks.