It’s been quite a while since we have heard the news about Google deprecating the
LocalBroadcastManager library. If you have been using it you have to find a better design alternative for your project. Here I am not touching on how to make your architecture better. But what can you use for drop in replacement for
LocalBroadcastManager. So let us see what we can do about the why Android LocalBroadcastManager is Deprecated.
Google is famous for it’s deprecation process. It deprecates Android components ridiculously fast. If you have not caught up with the other news. You can read my other article : Async Task in Android is Deprecated: There are Better Ways. According to google you should be using some form of
LiveData variation to achieve what the
LocalBroadcastManager did. I will write an article about how you can use
MVVM. But here let us focus on what can be used in place of
– What was LocalBroadcastManager ?
– The decision to deprecate LocalBroadcastManager
– What is the suggested implementation ?
– What can we use instead of LocalBroadcastManager ?
– Event Bus
– Singleton Observer
– Live Data
What was LocalBroadcastManager ?
LocalBroadcastManager was a application wide event-bus. Meaning it used to broadcast the events to all the listening components. Think of it as a subscriber listening for updates. But it used to listen to whole application for events.
It helped us make our task easier by allowing communication over components which cannot be directly linked. The components that exist in the same application but cannot be directly accessed. For example:
Activity. The communication is really tiring if you are just starting out with android development.
This was solved gracefully by Android LocalBroadcastManager but is now deprecated. Let us quickly go through why the library was deprecated in the first place ?
Also Read : How to get current GPS location in AndroidTECHENUM
The decision to deprecate LocalBroadcastManager
Okay if you do not really keep updated with the news from Android community. You might not know why the LocalBroadcastManager was deprecated. Let me paraphrase what they had to say about this decision.
- LocalBroadcastManager being a application wide event-bus, any component could catch what it was broadcasting.
- It had unnecessary use-case limitation because it inherited
BroadcastManagerand had to involve
Intents even when they were unnecessary.
- The reasons above made the development experience really very confusing ( really ? )
That was what they had to say to justify their actions, fair enough.
What is the suggested implementation ?
They have provided a better solution to this deprecated LocalBroadcastManager.
They want developers to use variations of
Observer pattern. One of the best implementation being
LiveData. Because it is life-cycle aware component which allows communication between background and foreground in a non blocking way.
The other solution they have mentioned is the use of Reactive Extensions (Rx). This is their way of handling the deprecation. Which I think is not very beginner friendly.
But then you might ask: where is your beginner friendly solution ?
Also Read : Learn How To Create Tabs With ViewPager2TECHENUM
What can we use instead of LocalBroadcastManager ?
So finally after all the boring stuffs we are here. Let us see what can we do about LocalBroadcastManager.
Okay so we are back to where we started. But didn’t we just read that event bus approach for LocalBroadcastManager was deprecated ?
Technically yes, but that doesn’t mean event bus is a bad approach. The deprecation really just means that they had stopped maintaining the library’s source code withing Android.
You are free to use the libraries as long as they are secure. And this is where the Greerobot’s Event Bus comes in.
According to them :
And it is really very small only ~60KB. Not only small it is also really very fast according to them.
The next approach would be creating a singleton object that instantiates when the application is created and runs as long as the application is running.
This looks like a harmless and normal approach. But you should really consider doing this when you have a larger code base. If you want cleaner code base these kind of global variable initialization is really very bad.
The developers will get frustrated about the integrity of the value. But even with all the bad things tied to it. It can be a quick and dirty way to get the Job done.
Also Read : Learn Android Snackbar the Material Design WayTECHENUM
This is officially suggested way to do what LocalBroadcastManager did. You can use
ViewModel with the
The only problem with this approach is you cannot use this anywhere outside the
So, if you are thinking of using LiveData for communication between you common classes. You cannot do it without a middle layer, be it the Activity or the Fragment itself.
So that is all I have for this article. I will bring a PoC for all these things I have mentioned and drop the link above. So that way you can follow along with what I have to offer.
Also if you have any good alternative to what I have already mentioned. Please feel free to comment below.
Keep learning more about Android LocalBroadcastManager Deprecated alternatives!
Also Read : Consume an API with Retrofit in AndroidTECHENUM