美文网首页
Fragment通过广播刷新界面

Fragment通过广播刷新界面

作者: 随心随性_0a25 | 来源:发表于2017-09-11 10:39 被阅读0次

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

    相关文章

      网友评论

          本文标题:Fragment通过广播刷新界面

          本文链接:https://www.haomeiwen.com/subject/mkixsxtx.html