EventBus简单使用

public class MainActivity extends AppCompatActivity {

    private TextView text_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text_view = findViewById(R.id.text_view);

    }

}

    创建一个bean类 作为发送的数据
 EventBus.getDefault().post(new ToastEvent("这是发送过来的数据"));

         //注册
         EventBus.getDefault().register(this);
/**
 * 具体注册了对什么事件的订阅,这个需要onEvent()方法来说明
 * @param toastEvent
 *
 * ThreadMode.MAIN不管消息在哪里发送 都在主线程接收这个事件
 *  ThreadMode.BACKGROUND
 *  Async处理事件会开启一个新的线程 这种模式可以耗时操作
 */
@Subscribe(threadMode = ThreadMode.MAIN,priority = 1000)
public void onToastEvent(ToastEvent toastEvent) {//参数就是订阅的事件(其实就是传递数据的类)
        //Toast.makeText(this,toastEvent.getContent(),Toast.LENGTH_SHORT).show();
        //取消事件的传送
        EventBus.getDefault().cancelEventDelivery(toastEvent);
        button.setText(toastEvent.getContent());
        }

/**
 * 粘性事件
 */
public class ThirdActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
    }


    public void post(View view) {
        //发送的是粘性事件
        EventBus.getDefault().postSticky(new ToastEvent("粘性事件的发送"));
    }


    public void dingyue(View view) {
        EventBus.getDefault().register(this);


    }


    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onToastEvent(ToastEvent toastEvent) {
        Toast.makeText(this,toastEvent.getContent(),Toast.LENGTH_SHORT).show();
    }


    public void yiChu(View view) {
        //移除
        EventBus.getDefault().removeStickyEvent(ToastEvent.class);
        EventBus.getDefault().removeAllStickyEvents();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();


        EventBus.getDefault().unregister(this);
    }

    public class MainActivity extends AppCompatActivity {




        private Button button;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            button = findViewById(R.id.button);


            //注册事件
            EventBus.getDefault().register(this);


            getSupportFragmentManager().beginTransaction().replace(R.id.frame_content,new DingYueFragment()).commit();


        }


        /**
         * 具体注册了对什么事件的订阅,这个需要onEvent()方法来说明
         * @param toastEvent
         *
         * ThreadMode.MAIN不管消息在哪里发送 都在主线程接收这个事件
         *  ThreadMode.BACKGROUND
         *  Async处理事件会开启一个新的线程 这种模式可以耗时操作
         */
        @Subscribe(threadMode = ThreadMode.MAIN,priority = 1000)
        public void onToastEvent(ToastEvent toastEvent) {//参数就是订阅的事件(其实就是传递数据的类)
            //Toast.makeText(this,toastEvent.getContent(),Toast.LENGTH_SHORT).show();
            //取消事件的传送
            EventBus.getDefault().cancelEventDelivery(toastEvent);


            button.setText(toastEvent.getContent());


        }


        //接触事件的订阅
        @Override
        protected void onDestroy() {
            super.onDestroy();


            EventBus.getDefault().unregister(this);
        }


        public void tiaoZhuan(View view) {


            Intent intent = new Intent(this,SecondActivity.class);
            startActivity(intent);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值