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.
Asked
Active
Viewed 7,482 times
5

Martin
- 2,146
- 4
- 21
- 32
1 Answers
6
I can only answer about LocalBroadcastManager.
- Add support library to your SDK and then to your Android project.
- Obviously, import the support library in your java class, etc.
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!

Radu
- 2,076
- 2
- 20
- 40
-
easier said than done... I've tried to add the damn library for hours, and I still don't have access to the damn class – Electric Coffee Jan 13 '14 at 03:03