Skip to main content

How to use EventBus for communication between activities in Kotlin Android

How to use EventBus for communication between activities in Kotlin Android.

Here's a detailed step-by-step tutorial on how to use EventBus for communication between activities in Kotlin Android.

Step 1: Add EventBus Dependency

To get started, you need to add the EventBus dependency to your project. Open your app-level build.gradle file and add the following line of code in the dependencies section:

implementation 'org.greenrobot:eventbus:3.2.0'

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

Step 2: Create Event Class

Next, you need to create an event class that will be used to pass data between activities. An event class is a simple Kotlin class that defines the data you want to send. For example, let's say you want to send a user object from one activity to another. You can create a UserEvent class like this:

class UserEvent(val user: User)

Step 3: Subscribe to Events

Now, you need to subscribe to events in the activity where you want to receive the data. To do this, add the @Subscribe annotation to a method in your activity class. This method will be called when an event is posted. For example:

@Subscribe
fun onUserEvent(event: UserEvent) {
val user = event.user
// Handle the user event here
}

Make sure to register and unregister the activity as an EventBus subscriber in the onStart and onStop methods respectively:

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

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

Step 4: Post Events

To send an event from one activity to another, you need to post the event using EventBus. In the activity where you want to send the event, simply create an instance of the event class and use the EventBus.getDefault().post() method to post it. For example:

val user = User("John Doe")
val event = UserEvent(user)
EventBus.getDefault().post(event)

Step 5: Handle Events

Once the event is posted, the method annotated with @Subscribe in the receiving activity will be called automatically. You can then handle the event and access the data. For example, in the onUserEvent method, you can update the UI with the user data:

@Subscribe
fun onUserEvent(event: UserEvent) {
val user = event.user
textView.text = user.name
}

That's it! You have successfully implemented EventBus for communication between activities in Kotlin Android. Remember to always register and unregister your activity as an EventBus subscriber to avoid memory leaks.

Conclusion

In this tutorial, you learned how to use EventBus to communicate between activities in Kotlin Android. EventBus provides a simple and efficient way to pass data between components without the need for explicit callbacks or interfaces. It promotes loose coupling and simplifies the code by decoupling the sender and receiver. EventBus is a powerful library that can greatly improve the communication between your activities in an Android application.