Fragment间的广播消息接收

这种方式不用在配置文件加东西,我比较喜欢。

广播注册,可以写在Activity(onCreate),也可以写在Fragment(onActivityCreated)里。

LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction("android.intent.action.CART_BROADCAST");//建议把它写一个公共的变量,这里方便阅读就不写了。

BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {

            @Override

            public void onReceive(Context context, Intent intent){

                System.out.println("OK");

            }

 };

 broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);

 

 


发送广播

Intent intent = new Intent("android.intent.action.CART_BROADCAST");

LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);

intent怎么传参数就不用说了。

 

你可能感兴趣的:(Fragment)