超简单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;
        }
    }
}

你可能感兴趣的:(超简单EventBus使用)