I was trying to find answer how to use LocalBroadcastManager
in class that extends BaseAdapter
but after hours spent by doing research how to do it I didn't find right answer. I need to call method from Fragment
inside my adapter, but I don't know how to do it. Can someone explain me how should I do it? Thank you for response and help.
For performing a specific action using Broadcast manager we have to firstly register our Broadcast and then only we can use this broadcast to receive updates. There are 2 ways to register our broadcast we can register it in our Android Manifest file or in our Activity where we want to receive its updates.
LocalBroadcastManager is an application-wide event bus and embraces layer violations in your app: any component may listen events from any other.
If you want some kind of broadcasting in your application then you should use the concept of LocalBroadcastManager and we should avoid using the Global Broadcast because for using Global Broadcast you have to ensure that there are no security holes that can leak your data to other applications.
localbroadcastmanager has been fully deprecated. There will be no further releases of this library. Developers should replace usages of LocalBroadcastManager with other implementations of the observable pattern. Depending on the use case, suitable options may be LiveData or reactive streams.
I can only answer about LocalBroadcastManager.
Declare your LocalBroadcastManager and instance it. Also, you can declare it static and use it throughout your (entire) app. So you don't have to instance a new one for every activity, etc.
public static LocalBroadcastManager mBroadcaster;
mBroadcaster = LocalBroadcastManager.getInstance(yourAppContextHere);
In every activity, service, etc, register and unregister a Receiver according to each's lifecycle. Each receiver can have different filters. For example in onResume (or onCreate) and onPause (or onDestroy)::
IntentFilter mFilter = new IntentFilter(MY_ACTION_1);
mFilter.addAction(MY_ACTION_2);
mBroadcaster.registerReceiver(localBluetoothReceiver, mFilter);
mBroadcaster.unregisterReceiver(localBluetoothReceiver);
And, finally, sending broadcasts and receiving with the receiver:
Intent sendCmdIntent = new Intent("your.package.name.your.action.name");
sendCmdIntent.putExtra(key, value);
mBroadcaster.sendBroadcast(sendCmdIntent);
private BroadcastReceiver localBluetoothReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Do whatever depending on the action and on the extra stuff you put in the intent.
All this is quoted from memory, feel free to edit it!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With