Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalBroadcastManager vs using callbacks

Android's compatibility pack supports LocalBroadcastManager, which enables sending broadcasts within my process. http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

Until now I was using callbacks (Interfaces similar to OnClickListener) to transfer data (asynchronous and synchronous) between different parts of my apps.

I was wondering if one is better then the other. Any opinions?

like image 759
dors Avatar asked Dec 19 '12 09:12

dors


2 Answers

LocalBroadcastManager lets you use Intent's and IntentFilter's so its easier to migrate from system-wide broadcasts to local ones. It also has some queuing code and might be more reliable than your own code (or not, depending how sophisticated your implementation is). Other than that, it essentially just keeps lists of receivers in memory and iterates them to find a match.

Another, alternative is to use an event bus such as Square's Otto (based on Guava), which adds type safety and is just as efficient.

like image 77
Nikolay Elenkov Avatar answered Oct 05 '22 18:10

Nikolay Elenkov


As far I've known, LocalBroadcastManager works like a charm. It's hassle free and you can pass any argument within Intent and retrieve it back during Listening. The only reliability is the broadcast manager puts intent into queue.

When should you use LocalBroadCastManager? When you have single activity (FragmentActivity) and tons of Fragment classes, then it's easier to have a localBroadcastManager within the Single Activity.

If you have a lot of Activities then using this may be helpful, but also keep in mind that you are already using intents to launch new activities, so if there is any pending intent then, this Broadcast will be in Queue and you will need to wait.

So, the best use is Single Activity with numerous Fragments.

like image 24
zIronManBox Avatar answered Oct 05 '22 18:10

zIronManBox