android从fragment进入activity再返回实现刷新fragment的做法

开发中有一种情况,就是从fragment中跳转到activity后,activity结束返回fragment的时候,如果想重新刷新fragment中的数据请求,可以用广播的方法来实现。

例子:

Fragment:

//fragment重新刷新的方法
@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)){
                refresh();
            }
        }
    };
    broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
}

private void refresh() {
    initview();
    initData();
}

注意:再refresh()中加入你重新初始化界面的操作。

接着,再activity中finish()前加:

Intent intent = new Intent("android.intent.action.CART_BROADCAST");
intent.putExtra("data","refresh");
LocalBroadcastManager.getInstance(postmanaddActivity.this).sendBroadcast(intent);
sendBroadcast(intent);

 

展开阅读全文

没有更多推荐了,返回首页