简单使用EventBus,告别繁琐的接口回调(2)

接上篇,学习如何使用EventBus。

如果是用Android Studio开发的话,在Gradle文件中 

compile 'org.simple:androideventbus:1.0.5'

即可使用了。下面贴代码:

public class SubscriberActivity extends AppCompatActivity {
    public static final String GET_MESSAGE_SUCCESS ="get_message_success" ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_subscriber);
        if (isUseEventBus()){
            EventBus.getDefault().register(this);
        }
        initView();
    }

    protected boolean isUseEventBus(){
        return true;
    }

    private  TextView get_msg_tv;
    private void initView() {
        Button jump_btn = (Button) findViewById(R.id.jump_btn);
        jump_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SubscriberActivity.this,RxJavaActivity.class);
                startActivity(intent);
            }
        });
       get_msg_tv = (TextView) findViewById(R.id.get_msg_tv);
    }


    @Subscriber(tag = GET_MESSAGE_SUCCESS)
    private void getMessageSuccess(String msg){
        get_msg_tv.setText(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isUseEventBus()){
            EventBus.getDefault().unregister(this);
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button android:id="@+id/jump_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="jump to RxJavaActivity"/>
    <TextView android:id="@+id/get_msg_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="未收到msg的textView"/>

</LinearLayout>

这是入口Activity。

public class RxJavaActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_rxjava);
        initView();
    }


    private void initView() {
        final String msg = "由RxJavaActivity发送给SubscriberActivity的消息";
        Button send_msg_btn = (Button) findViewById(R.id.send_msg_btn);
        send_msg_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(msg,SubscriberActivity.GET_MESSAGE_SUCCESS);
            }
        });
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button android:id="@+id/send_msg_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send message to activity first"/>

</LinearLayout>

这是返回msg的Activity。

EventBus使用就是这么简单的一句 

 EventBus.getDefault().post(msg,SubscriberActivity.GET_MESSAGE_SUCCESS);

参数一是你需要传递过去的数据data,参数二是tag,自定义一个常量字符串即可。

这是发送,那怎么接收呢

@Subscriber(tag = GET_MESSAGE_SUCCESS)
    private void getMessageSuccess(String msg){
        get_msg_tv.setText(msg);
    }

注解Subscriber,tag=上面自定义的字符串常量,方法名自定义(其实没什么作用),而EventBus识别具体从哪个EventBus源(一个项目中肯定有多出Eventbus的post)post过来的通过①tag②方法的参数。

当他检查到tag一致,参数(post方法的第一个参数与自定义方法的参数一致)一致,即能响应。

当然最关键的一步:注册。

EventBus.getDefault().register(this);

这一步必不可少,否则无效。而上面贴的代码里我们自定义了一个方法 isUseEventBus ,当我们开发一个项目的时候必然有一个基类,直接将注册写在基类中,通过这个方法决定你的子类要不要使用EventBus即可。

最后别忘了在 onDestroy 方法中注销EventBus,这是个好习惯(涉及到内存泄漏问题)。

到此我们就会发现不需要你自定义接口,不需要用Handler处理子线程主线程等繁琐的问题,确实比接口回调省事多了。虽然栗子很简单,但是在实际项目中有更大的使用价值。

原文地址:https://www.cnblogs.com/cherrylv/p/6433152.html