Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalBroadcastManager for multiple intents

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?

like image 325
masmic Avatar asked Jul 24 '14 07:07

masmic


1 Answers

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
        }
    }
};
like image 153
masmic Avatar answered Oct 22 '22 06:10

masmic