How to use LocalBroadcastManager?
LocalBroadcastManager
allows to register and send broadcasts to local objects within your application.
Advantages over sending Global Broadcasts:
·
No need to worry about leaking of data as the
data you are broadcasting never leave your app.
·
It is more efficient than global broadcast.
·
Not possible for other applications to send
these broadcasts to your applications.
- Create receiver.
private
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void
onReceive(Context context, Intent intent) {
//
your code
}
}
- Register receiver
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter("loc_receiver"));
- Send Broadcast
Intent intent = new Intent("loc_receiver");
LocalBroadcastManager.getInstance
Comments
Post a Comment