EventBus is a library that enables notification between Activity, Fragment or something.

To use EventBus, first of all, add this line to app/build.gradle and sync.

dependencies {
// EventBus
implementation 'org.greenrobot:eventbus:3.1.1'

Secondly, define a Class to be notified. In this case, I defined a very simple Class with one property. The instance is passed from EventBus to the Activity, Fragment or something.

class Event(val text: String)

To notify some event, just call the post method and pass the instance of the event. EventBus’ instance is obtained by EventBus#getDefault basically.


To receive the event, we need to register EventBus. We also need unregister upon destroying. @Subscribe annotation enable to receive the event. Any name is OK for the method with @Subscribe annotation.

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

// Subscribe Event

override fun onDestroy() {

// Unsubscribe Event

fun onEvent(event: Event) {
textView.text = event.text