Skip to content

Instantly share code, notes, and snippets.

@rameshvoltella
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rameshvoltella/5810531bde9d07fe7aec to your computer and use it in GitHub Desktop.
Save rameshvoltella/5810531bde9d07fe7aec to your computer and use it in GitHub Desktop.
LocalBroadcastManager  which used to update the one activity or fragment data from another one,Example if we have two activity A and B, from activity A navgate to activity B and if i do some change in activty b and if those changes should reflect on activity A the we can use the following Methord.the Advantage is that we can do this from any cla…
//ActivityA.java
@Override
public void onCreate(Bundle savedInstanceState) {
// Register to receive messages.
.
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
new IntentFilter("GetData"));//Getname is the action name this should important this action name should be same from activity b othereviser recever willnot recevs the data
}
// Received Intents will called whenever an Intent
// with an action named "GetData" is broadcasted from the Activity B
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String mDataFromActivityB = intent.getStringExtra("data");
Log.d("Data", "" + message);
}
};
@Override
protected void onDestroy() {
// Unregister when activity finished
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
super.onDestroy();
}
//ActivityB.java
// For Example here we notify the change in ActivityB to Activity A bya Button click.
public void mButtonClick(View v)
{
Log.d("sending", "data");
Intent mintent = new Intent("GetData");
mintent.putExtra("data", "changed the data");
LocalBroadcastManager.getInstance(this).sendBroadcast(mintent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment