纠结了好久的问题,fragment跳转到activity后,当activity执行某些操作后,当返回时,刷新当前的fragment.用广播实现。
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(UploadPictureActivity.this).sendBroadcast(intent);
sendBroadcast(intent);
finish();