Skip to main content

EventBus Events For Android

EventBus Event

EventBus: Events for Android

There are various ways the Android framework depend on passing data between the android components:

  • Activities use Intents to pass data between screens.
  • Fragments use Bundle to pass data and listener pattern for the Fragment to fire events.
  • Servies use Broadcast Managers to send data to other components.

One of the primary issues of these ways is that if they are tightly coupled together the effective communication between android components can become difficult.

  • Tight coupling:

Tight coupling is when a group of classes is highly dependent on one another.

  • Loose coupling:

Loose coupling means they are mostly independent. They are dependent on the interface rather than the class.

To Solve the above issues we have an Android EventBus library. EventBus is an open-source library that makes easier communication between Activities, Fragments, Threads, and Services. EventBus use the publisher/subscriber pattern. These patterns try to avoid tight integration (tight coupling).

In the pattern, Publishers are responsible for posting events in response to a state change, while Subscribers respond to these events.

Image removed.

There no exists direct coupling between the code of publisher of the event and code that receive it. Using EventBus, you can pass messages from one class to one or more classes.

Add the dependency

The first step to use the EventBus library is to add the following dependency in build.gradle (module-level) of our project.

Image removed.

Posting Events

This section defines how to send any event with EventBus. This event will be received by all registered event handlers in the application. Let us first define our custom event class. This is a simple POJO or data class that can be customized as per our use

Image removed.

Subscribing to Events

This section defines how to listen to posted events. To be a subscriber, you have to do three main things:

1. Register the subscriber in the event bus with register(). This method registers your component to subscribe to events. Inactivity, you put this method in the onStart()

override fun onStart() {

super.onStart()

EventBus.getDefault().register(this)
}

2. Unregister the subscriber on the event bus with unregister(). This method stops receiving events on the bus. Inactivity, you put this method in the onStop()

override fun onStop() {

super.onStop()

EventBus.getDefault().unregister(this)
}

3. The most important method is onMessageEvent(), this method is use to receive events from event bus. You have to declare a method inside your subscriber class ( In which you want to receive value from event) with the name onMessageEvent() for every type of event you want to subscribe:

@Subscribe(threadMode = ThreadMode.MAIN)
 fun onMessageEvent(messageEvent:MessageEvent){
Log.d(“TAG”,messageEvent.message)
}

From the above methods, you received your custom class instance in a parameter. Now you can access                    your value from an instance like:

instance_name.attribute_name

EventBus Thread Modes

Posting   

@Subscribe(threadMode = ThreadMode.POSTING)

This is the default. Subscribers will be called in the same thread as the thread where the event is posted. This mode indicates the least overhead because it avoids thread switching completely. If tasks that are known to complete in a short time without requiring main thread then you can use these modes.

Main 

@Subscribe(threadMode = ThreadMode.MAIN)

In this mode, subscribers will receive events in the main UI thread.Event handlers using this mode must return quickly to avoid blocking the main thread.

Background 

@Subscribe(threadMode = ThreadMode.BACKGROUND)

This mode is the same as ThreadMode.POSTING but the difference is that if an event is posted in the main thread, then subscribers will get them on a background thread. If the posting thread is the main thread, EventBus uses a single background thread that will deliver all its events sequentially.

Async 

@Subscribe(threadMode = ThreadMode.ASYNC)

In this mode, subscribers will always receive events independently from the current and main thread. This is useful for long-running operations like network operations. Avoid triggering a large number of long-running asynchronous handler methods at the same time to limit the number of concurrent threads.

Subscriber Priorities

If you want to change the order in which subscribers get events, then you need to specify their priority levels during registration. Within the same delivery thread (ThreadMode), higher priority subscribers will receive events before others with a lower priority. The default priority is 0.

@Subscribe(priority = 1)
      fun onMessageEvent(messageEvent:MessageEvent){
Log.d(“TAG”,messageEvent.message)
}

Note: the priority does not affect the order of delivery among subscribers with different ThreadModes!

Tags
  • Website Development