Android Fragment間的廣播消息接收
阿新 • • 發佈:2017-11-03
tac contex ger 配置 man gist spa override div
這種方式不用在配置文件加東西,我比較喜歡。
廣播註冊,可以寫在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);
Android Fragment間的廣播消息接收