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.

LocalBroadcastManger is introduced in android 3.0, so you have to use android support library v4 for prior releases.
  • 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

Popular posts from this blog

All about Context

All about AsyncTask