1.在Fragment的onActivityCreated()方法中注册广播
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
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){
String msg = intent.getStringExtra("data");
if("refresh".equals(msg)){
getData();}}};
broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
}
2.在Activity结束的时候更新
Intent intent = new Intent("android.intent.action.CART_BROADCAST");
intent.putExtra("data","refresh");
LocalBroadcastManager.getInstance(Activity.this).sendBroadcast(intent);
sendBroadcast(intent);
finish();
网友评论