Skip to main content

How to use EventBus for inter-fragment communication in Kotlin Android

How to use EventBus for inter-fragment communication in Kotlin Android.

Here's a step-by-step tutorial on how to use EventBus for inter-fragment communication in Kotlin Android.

Step 1: Add EventBus to your project

To use EventBus in your Android project, you need to add the EventBus dependency to your project's build.gradle file. Open the build.gradle file of your app module and add the following line in the dependencies section:

implementation 'org.greenrobot:eventbus:3.2.0'

Sync your project to download the dependency.

Step 2: Create an Event class

In EventBus, communication between different components is done through events. An event is a simple data class that holds the information you want to pass between fragments. Create a new Kotlin file and define an event class. For example:

data class MessageEvent(val message: String)

Step 3: Register and unregister the event bus

To receive events, each fragment needs to register itself with the event bus. In the fragment's onCreate() method, call the EventBus.getDefault().register() method to register the fragment as a subscriber. Similarly, in the fragment's onDestroy() method, call the EventBus.getDefault().unregister() method to unregister the fragment. Here's an example:

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

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

Step 4: Subscribe to events

To receive events in a fragment, you need to create a method annotated with the @Subscribe annotation. This method will be called whenever an event of the specified type is posted to the event bus. For example:

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

Step 5: Post events

To send an event from one fragment to another, you need to post the event to the event bus. In the sending fragment, use the EventBus.getDefault().post() method to post the event. For example:

val message = "Hello from Fragment A!"
val event = MessageEvent(message)
EventBus.getDefault().post(event)

Step 6: Handle events in the receiving fragment

In the receiving fragment, the onMessageEvent() method will be called when the event is posted. You can access the event data and perform the necessary actions. For example:

@Subscribe
fun onMessageEvent(event: MessageEvent) {
val message = event.message
// Update UI or perform other actions based on the message
}

Step 7: Test the event communication

Run your app and test the event communication between fragments. When an event is posted, the receiving fragment will handle the event and update its UI or perform other actions based on the event data.

That's it! You have successfully implemented inter-fragment communication using EventBus in Kotlin Android. EventBus simplifies the communication between components by allowing them to send and receive events without directly interacting with each other.