EventBus粘性事件---Demo

MainActivity

//发送粘性事件
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void sendMewssage(MessageEvent messageEvent){
        event_text.setText(messageEvent.getMessage());

    }

//发送粘性事件到接收页面
            EventBus.getDefault().postSticky(new MessageEvent("从主页面发送过来的粘性事件"));
            Intent intent = new Intent(MainActivity.this, EventBusActivity.class);
            startActivity(intent);
//解除事件
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().removeStickyEvent(this);
        EventBus.getDefault().unregister(this);
    }

EventBusActivity

//接收粘性事件
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void receiveMewssage(MessageEvent messageEvent){
        event_text2.setText(messageEvent.getMessage());
    }
    //判断是否注册
if (!EventBus.getDefault().isRegistered(this)){
          EventBus.getDefault().register(this);
    }

你可能感兴趣的:(EventBus粘性事件---Demo)