Skip to main content

LocalBroadcastManager in Android

What is LocalBroadcastManager and How to use LocalBroadcastManager in Android

Today, we are going to learn what is localbroadcastmanager and how to use it our application.

Let's start.

Whats is LocalBroadcastManager? 

Just like Global Broadcast receiver where we receive various broadcasts from different applications. 

For example, we can the listener broadcast for battery percentage when it very low or we can set various listeners for various events.

We can also make our custom broadcast and can set the receiver from other applications and can communicate between different applications.

But what if you only want that your custom broadcast can only listen within the application to pass some sensitive data something like user data or some transaction information.

The problem with global broadcast is that it is system-wide and can be vulnerable which can be very harmful especially when we are working with some sensitive data.

By using local broadcast we can pass data from one point to another point within the application without informing the system-wide.

Let's see an example to understand it better.

How to use LocalBroadcastManager?

Here is an example where we are consuming LocalBroadcastManager in our application to show the use of it.

First, we have a text view to show the message and a button to start an activity from where we will change the first activity text view value by using localbroadcastmanager.

Check this gif here.

Create a new project in the android studio if you not created already.

To use localbroadcastmanager in your application we need to add this dependency in our application. 

Open your build.gradle (Module: app) file and add this line in the dependencies section.

After adding the dependency hit the sync button.

After that, open your activity xml file and add a text view and a button.

Like this.

Here, we set the id textView to our text view and starSecondActivity to button.

Now open your activity java file and add this code in the file.

First, we declare TextView at the first and in the onCreate method, we initialize the text view by using the findViewById method.

After that, we initialize the button and then set the click listener to start the activity.

Now it comes the main part, we are initializing the localBroadcastManager and initialize the broadcast receiver and set that receiver to localBrasdcastManager.

In the receiver, we are getting the data that we can send along with the intent.
and then set that data to our text view and show a toast.

This was the receiver part now it comes where we send the broadcast with some data.

To do this we need to create second activity and we name it as SecondActivity.

Open the second activity xml file and add a button to send the broadcast like below.

We set the id to the button as setButton.

After adding the button, open your java file and initialize the button and set the click listener and in the listener we write the code to send a broadcast. Like below.

In the button listener, we initialize the LocalBroadcastManager and then we set new intent for local broadcast with our custom action name as "CUSTOM_ACTION". You can change it and set custom action name whatever you like.

Now, put the data in the intent by using the putExtra method which accepts two parameters as key-value like data. We set the key as "DATA" and set the value "New Value from Second Activity". 

You are free to change these values according to your choice.

Now its time to send the local broadcast by using the sendBroadcast method of LocalBroadcastManager and pass our intent in the parameter of sendBroadcast.

After sending the broadcast we need to specify which broadcast we should listener in our first activity.

To do this, we need to open our first activity java file and register receiver with custom intent action after our broadcast receiver like this.

In the above code, we are calling the registerReceiver method which accepts parameters, the first parameter is for our BroadcastReceiver and the second is for the intent filter with the name of our intent action name as we set our action as  "CUSTOM_ACTION" as above.

See also: Pass data one activity to other activity

Now run our application, after launching the application you will see the text view with the text of hello world and a button, click on the button to start the second activity and click on the Set button.

After clicking you will see a toast message which says "Text Set!" and if you press the back button, your first activity text view has changed with the data we send with the local broadcast.

Now, this comes to the end of the tutorial, thank you for reading.

If you learned something new, share this article on what is LocalBroadcastManager and how to use it, with your friends and mates.

Popular Posts

Working with Bottom Navigation bar in Android [Full Guide]

Custom Switch in Android | | Android Tutorial

Customize rating bar in android