Rxbus 的使用

2017-05-18  本文已影响93人  小菜_charry

背景介绍:
在需要多个界面之间传递信息使用,rxbus简便、简单。

发送方-UserBindPasswordActivity:

在绑定密码成功之后,需要把成功的消息发给用户信息页面

    @Override
    public void updatePasswordSuccess() {
        RxBus.getDefault().post(new Events.UserBindPassword(true));
        finish();
    }

接收方-UserInfoActivity :

用户信息页面接收到成功绑定的消息,做相应的操作。


    // 重新设置密码成功的回调
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void event(Events.UserBindPassword password) {
        if (password.isResetPasswordSuccess()) {
            mTvLoginPwd.setText("修改");
            mTvLoginPwd.setTextColor(getResources().getColor(R.color.black));
        }
    }

消息实体类-Events :

需要一个实体类来区别不同的消息,在Events类中定义一个内部类:


    public static class UserBindPassword {
        boolean resetPasswordSuccess;
        public UserBindPassword(boolean resetPassword) {
            this.resetPasswordSuccess = resetPassword;
        }
        public boolean isResetPasswordSuccess() {
            return resetPasswordSuccess;
        }
        public void setResetPasswordSuccess(boolean resetPasswordSuccess) {
            this.resetPasswordSuccess = resetPasswordSuccess;
        }
    }

最后还需要注册及取消注册:

   @Override
    protected void onStart() {
        super.onStart();
        RxBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unRegister(this);
    }

基于:
compile 'com.wzgiceman:RxBus:1.0.2'

上一篇下一篇

猜你喜欢

热点阅读