如何从fragment跳到activity再从activity返回(finish()方法返回)刷新fragemnt页面

代码改变世界

如何从fragment跳到activity再从activity返回(finish()方法返回)刷新fragemnt页面

广播方法实现Fragment页面刷新

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)){
                    refresh();
                }
            }
        };
        broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
    }

Activity中

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

  实现页面刷新,其中:

  refresh();

  这个方法是实现刷新功能,也就是你加载数据的方法。不要忘了控件重新初始化。

  

posted @ 2018-01-12 10:44  改变世界的老十七  阅读(4449)  评论(0编辑  收藏  举报