Send data from Activity to Fragment in Android using BroadcastReceiver. I know there is varies way to communicate Activity to Fragment.
But i don't know how to send data and receive using BroadcastReceiver.
From your Activity
Intent intent = new Intent("KEY");
sendBroadcast(intent);
In your fragment
private BroadcastReceiver mNotificationReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
updateUi();
}
};
#Override
protected void onResume() {
super.onResume();
registerReceiver(mNotificationReceiver, new IntentFilter("KEY"));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mNotificationReceiver);
}
Related
I am creating sending broadcast from activity to fragment. But in my case receiver called three times as I only send broadcast once. please help
Here , I am registering broadcast in Fragment
#Override
public void onResume() {
super.onResume();
getActivity().registerReceiver(broadcastReceiver, new IntentFilter("example.com"));
}
#Override
public void onPause() {
super.onPause();
getActivity().unregisterReceiver(broadcastReceiver);
}
And my receiver in Fragment
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Here I am receiving the data and want to update the list in
//my fragment
};
Sending the broadcast
Intent intent = new Intent("example.com");
intent.putExtra("data","any_data");
sendBroadcast(intent);
You must always set Action to Intent like below,
Intent intent = new Intent();
intent.setAction("action_type");
sendBroadcast(intent);
And in your receiver add this line :
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction("action_type")) {
// Write your code here now.
}
};
Hope it works.
I have a question about updating my ListView in ListFragment from my Map fragment. Each time I will call this method I will assign different values to String Array. And what should I put into that Update method to refresh the listview ?
In ListFragment create and register BroadcastReceiver :
static final String ACTION_CUSTOM = "action";
BroadcastReceiver mReceiver;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReceiver = new BroadcastReceiver(){
public void onReceive(android.content.Context context, Intent intent) {
//Check for action you need
if (intent.getAction() == ACTION_CUSTOM ){
//Do stuff with data from intent
intent.getExtras().getString("data");
}
}
};
}
#Override
public void onResume() {
super.onResume();
getActivity().registerReceiver(receiver, new IntentFilter(ACTION_CUSTOM ));
}
#Override
protected void onPause() {
super.onPause();
getActivity().unregisterReceiver(receiver);
}
In MapFragment send broadcast intent, when you need to change data:
Intent intent = new Intent();
//Set action you need to send
intent.setAction(ACTION_CUSTOM);
//Add data to send
intent.putExtra("data", "data_value");
//Send broadcast intent
getActivity().sendBroadcast(intent);
In my Application I have to notify my Activity from IntentService class .
For that purpose I am using LocalBroadcastManager. But I am not receiving anything in the onReceive of my Broadcast Receiver. Here is what I have written.
In my BaseActivity I have registered my receiver.
public class BaseActivity extends FragmentActivity implements App {
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
}
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter(custom-event-name));
}
#Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(
mMessageReceiver);
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
System.out.println("Overlay Message" +bundle.getString("message"));
}
};
}
I am sending a local broadcast from my RegisterAlarmIntentService class.
public class RegisterAlramIntentService extends WakefulIntentService implements
APIConstants {
public RegisterAlramIntentService() {
super("AlarmService");
}
#Override
public String getTag() {
return "AlarmService";
}
#Override
protected void onHandleIntent(Intent intent) {
System.out.println("Working till here fine In RegisterAlarm");
Bundle bundle = intent.getExtras();
Intent localIntent = new Intent(custom-event-name);
localIntent.putExtras(bundle );
LocalBroadcastManager.getInstance(this).sendBroadcast(
localIntent);
}
}
onHandleIntent() method is called. But nothing is received in onReceive() of my receiver.
Please Help. Thanks in advance!!
Try
public class RegisterAlramIntentService extends WakefulIntentService implements
APIConstants {
Intent localIntent;
public RegisterAlramIntentService() {
super("AlarmService");
localIntent = new Intent(custom-event-name);
}
#Override
public String getTag() {
return "AlarmService";
}
#Override
protected void onHandleIntent(Intent intent) {
System.out.println("Working till here fine In RegisterAlarm");
Bundle bundle = intent.getExtras();
Thread.sleep(5000); // For Testing only because it is in whole new thread (which may not wait for your reciever to setup)
localIntent.putExtras(bundle );
LocalBroadcastManager.getInstance(this).sendBroadcast(
localIntent);
}
}
Also in manifest :
<service android:name="com.commonsware.android.localcast.RegisterAlramIntentService"/>
See this
I'm working in an app where I have just implemented the ViewPager. I was first working with tabs but when I made the change to ViewPager I started having an issue with the data transfer from Activity to fragments.
I finally found this post where this issue was explained and where recomends to use LocalBroadcastManager to send data from Activity to Frament when using ViewPager. I have implemented this code and works fine:
Fragment1.java
public class Fragment1 extends Fragment {
public static final String ACTION_INTENT = "fragment1.action.BOX_UPDATE";
/*Register the receiver*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION_INTENT);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(ActivityDataReceiver, filter);
}
/*Broadcast Receiver*/
protected BroadcastReceiver ActivityDataReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(ACTION_INTENT.equals(intent.getAction())) {
String text = intent.getStringExtra("TEXT");
witeOnBox(text);
}
}
};
/*Method which changes the text with the icoming value*/
public void witeOnBox(String text) {
tv_log.setText(text);
}
/*Unregister receiver*/
#Override
public void onDestroy() {
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(ActivityDataReceiver);
super.onDestroy();
}
Now, in MainActivity I send the data to the fragment this way:
protected void sendValueToFragment1(String text) {
Intent intent = new Intent("fragment1.action.BOX_UPDATE");
intent.putExtra("TEXT", text);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
Now I need to send other data from MainActivity to the same Fragment1, and my question is, how do I manage in the fragment the incoming of another intent?
Do I have to create another BroadcastReceiver to manage a new Intent, or can I manage a new intent in the same BroadcastRecevier? I see that I can check using an if inside the onReceive() method of the BroadcastReceiver for the incoming intent filter, but If I declare two diferent intent filters in the same fragment, how do I asign them to the BroadcastReceiver?
I finally solved adding an Action to the previously defined IntentFilter:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION_INTENT);
filter.addAction(ACTION_INTENT2);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(ActivityDataReceiver, filter);
}
Then in the BroadcastReceiver I manage the incoming actions:
protected BroadcastReceiver ActivityDataReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(ACTION_INTENT.equals(intent.getAction())) {
//Do
}
if(ACTION_INTENT2.equals(intent.getAction())) {
//DO
}
}
};
Much better ways than using broadcast receiver,
Fragment frag = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + mPager.getCurrentItem());
if(frag!=null && mPager.getCurrentItem()==1){
((Fragment1) frag).setText(text);
}
If you navigate to the Fragment after this has been called you can have it get the variables from the Activity using getActivity();
I am trying to update the UI (Activity) after some action has been performed in the service. This is very simple example but it doesn't seem to work for me. What am I missing here?
ExampleService:
public class ExampleService extends IntentService{
#Override
protected void onHandleIntent(Intent intent) {
notifyActivity();
}
private void notifyActivity() {
Intent broadcast = new Intent(this, ExampleActivity.class);
sendBroadcast(broadcast);
}
}
ExampleActivity:
public class ExampleActivity extends ListActivity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
#Override
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
registerReceiver(receiver, filter);
}
}
You cannot send a broadcast to an anonymous dynamic receiver that way. You will need to define an action string in the Intent and use that action string in the IntentFilter.
You might consider using LocalBroadcastManager for this scenario, for better performance. Here is a sample project demonstrating this.