超简单EventBus使用

两个Activity之间的传值
第一个页面传值给第二个页面必须使用粘性事件
第二个页面传值给第一个页面可以使用粘性事件也可以不使用
1.导依赖

implementation 'org.greenrobot:eventbus:3.1.1'

2.bean类

public class Student {

    private String name;
    private String sex;

    public Student() {

    }

    public Student(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

3.MainAcitivity

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.send_btn)
    Button jumpBtn;
    @BindView(R.id.result_tv)
    TextView resultTv;
    @BindView(R.id.img_sim)
    SimpleDraweeView imgSim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //注册
        EventBus.getDefault().register(this);
       
    }
    
    @OnClick({R.id.send_btn, R.id.result_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.send_btn:
                EventBus.getDefault().postSticky(new Student("张三", "男"));
                startActivity(new Intent(this, EventBusActivity.class));
                break;
        }
    }

    //处理定义接受的方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void studentEventBus(Student student) {
        resultTv.setText(student.toString());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销注册
        EventBus.getDefault().unregister(this);
    }

}

4.EventBusActivity


public class EventBusActivity extends AppCompatActivity {

    @BindView(R.id.result_tv)
    TextView resultTv;
    @BindView(R.id.send_btn)
    Button sendBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_bus);
        ButterKnife.bind(this);
        //注册
        EventBus.getDefault().register(this);
    }

    //处理定义接受的方法
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void studentEventBus(Student student) {
        resultTv.setText(student.toString());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销注册
        EventBus.getDefault().removeAllStickyEvents();
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.send_btn)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.send_btn:
                EventBus.getDefault().postSticky(new Student("李四","男"));
                finish();
                break;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值