Fragment通过广播刷新界面

2017-09-11  本文已影响0人  随心随性_0a25

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

上一篇下一篇

猜你喜欢

热点阅读