LocalBroadcastManager用于多个意图

时间:2014-07-24 07:34:08

标签: android android-intent android-fragments

我正在一个应用程序中工作,我刚刚实现了ViewPager。我第一次使用制表符但是当我对ViewPager进行更改时,我开始遇到从Activity到片段的数据传输问题。

我终于找到了解释此问题的this post,并建议在使用ViewPager时使用LocalBroadcastManager将数据从Activity发送到Frament。我已经实现了这段代码并且工作正常:

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();
    }

现在,在MainActivity中,我以这种方式将数据发送到片段:

protected void sendValueToFragment1(String text) {
    Intent intent = new Intent("fragment1.action.BOX_UPDATE");
    intent.putExtra("TEXT", text);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
} 

现在我需要将MainActivity中的其他数据发送到同一个Fragment1,我的问题是,如何在片段中管理另一个意图的传入?

我是否必须创建另一个BroadcastReceiver来管理新的Intent,还是可以在同一个BroadcastRecevier中管理新的intent?我看到我可以在BroadcastReceiver的onReceive()方法中使用if来检查传入的意图过滤器,但是如果我在同一个片段中声明了两个不同的intent过滤器,我如何将它们与BroadcastReceiver对齐?

2 个答案:

答案 0 :(得分:17)

我终于解决了将一个Action添加到之前定义的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);
}

然后在BroadcastReceiver中我管理传入的操作:

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
        }
    }
};

答案 1 :(得分:-1)

比使用广播接收器更好的方法,

  Fragment frag = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + mPager.getCurrentItem());
            if(frag!=null && mPager.getCurrentItem()==1){
                ((Fragment1) frag).setText(text);
            }

如果在调用此片段之后导航到片段,您可以使用getActivity()从Activity中获取变量;