Search code examples
androidcallbackbroadcastreceiver

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?


Solution

  • 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.