Event Handling With LocalBroadcastManager Android

Event handling is a complex process when it comes to Android because we need to consider the lifecycle of the components too. In this blog post, we will be covering how we can use Android’s own LocalBroadcastManager API to send and receive events between different components of the Application.

From the official documentation

Helper to register for and send broadcasts of Intents to local objects within your process.

In simple words, LocalBroadcastManager is used to register BroadcastReceivers which are based on the publish-subscribe pattern. Meaning we can publish events from one place and can listen to them at another.

Let’s take a scenario where we can get the most out of it. Below are the screenshots of my app SpeakOut.

On the first screen, I have a post that we can like, bookmark or delete. Now if I click on the profile icon it will take me to the second screen which is Profile Screen. From Profile Screen if I again click on any post that will take me to the last screen which is Post View Screen.

Here comes the main part. Let’s say if I like the post in Post View Screen it should be updated at all the previous screen also, right? There are multiple ways to implement this but for simplicity, I have used LocalBroadcastManager in my app.

To implement this, we need to create a LocalBroadcastManager and register it in the onCreate(depending on the use case) method of our Activity or Fragment. To avoid memory leaks, it is recommended to unregister it when it’s no longer needed. For eg., if you don’t want to receive any events if your Fragment’s onStop is called, unregister it there.

In the above code snippet, I have created a Manager class to handle different types of Broadcast listeners. In this blog, I will be covering the Post BroadcastManager.

PostEvents.kt

The above code snippet is an implementation of the EventBroadcastManager. The Broadcast is registered in the init block means whenever an instance of PostEvents is created. I have made the sendEvent method a companion object so that we can call it from anywhere without creating an instance of PostEvents and also achieve abstraction.

Now in your Fragments/Activities, create an instance of PostEvents like below:

HomeFragment.kt

BroadcastReceivers provides an intent object as a result so we need to extract values from that.

We will need to add the above snippet at all the places where we want to listen to the post events. Now we are all set up to listen to the events, the only part left is sending those events. Let’s get on to that now.

Here it is…

PostViewFragment.kt

This method will send events to all the components which are listening to it.

This is how we can use LocalBroadcastManager as an Event handling mechanism in our app. For a complete example, refer to my app.

Thanks for reading :)

Android Developer at upGrad

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store