Broadcast intent vs multicast listener implementation on Android

405 views Asked by At

I want to enable for several objects to subscribe to an event at the same time.

Currently I see two approaches: to send broadcast intents, or to implement multicast listeners (a manager class containing an array of the subscribed listeners, and with subscribing and unsubscribing functionality) such as the built-in events in C#.

Which is the best practice on Android? Or is there any other approach worth considering?

3

There are 3 answers

1
AquaJava On BEST ANSWER

You may try LocalBroadcastManager, which is a lightweight solution for sending broadcast intents, and it is a part of Android support library v4.

0
CommonsWare On

Which is the best practice on Android?

If you are trying to do this between apps, use broadcasts.

If you are trying to do this within one app, use an existing event bus implementation, such as Otto or greenrobot's EventBus, rather than reinventing that wheel.

0
siva On

Subscribing each object to a broadcast receiver may not be a good idea because the execution sequence is unknown. At the top level you can have a single broadcast receiver which will receive the intent and have a logic to notify all the listeners inside it.