近两天学习了一个新的框架,就是EventBus,它可以取代intent,实现在不同组件之间进行数据传递。这里只是简单记录一下它的使用方法。以备不时之需。
第一步:导入EventBus的jar包,直接将jar拷到工程目录的libs文件夹下即可。
第二步:就是在代码中使用了。
总共分四步:
1. 注册
2. 发送消息
3. 接收消息
4. 注销
具体如下:
第一步:注册,直接在onCreate方法中进行。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
}
第二步:发送消息,因为这个是发送方,所以在另一类中,这里先贴出发送消息的代码,下面会贴出完整代码:
public void toSubscriber(View v)
{
EventBus.getDefault().post(this);
finish();
}
第三步:接收,在接收消息的类中复写框架中的前缀为onEvent方法,此方法有四种,按需求复写即可。这里就用下面这种:
public void onEventMainThread(Publisher publisher)
{
Toast.makeText(this, publisher.txt, 0).show();
}
上面的方法中的参数可以是任意类型,可以自定义,也可以是发送方本身及其中的成员。本例中发送的就是发送方本身对象。
第四部:注销,在接收消息的类中复写onDestroy方法,则其中完成注销。
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
下面贴上完整代码:
本例完成的功能就是从MainActivity跳转到Publisher,当从Publisher返回时,MainActivity通过方法onEventMainThread拿到Publisher的实例。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
android:orientation="vertical" >
<!-- android:id="@+id/toPublisher" -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#77ff0000"
android:onClick="toPublisher"
android:text="Publisher" />
</LinearLayout>
MainActivity.java
package com.demo.eventbus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import de.greenrobot.event.EventBus;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
}
public void onEventMainThread(Publisher publisher)
{
Toast.makeText(this, publisher.txt, 0).show();
}
public void toPublisher(View v)
{
startActivity(new Intent(this,Publisher.class));
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
publisher.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
android:orientation="vertical" >
<!-- android:id="@+id/toSubscriber" -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:onClick="toSubscriber"
android:text="Subscriber" />
</LinearLayout>
Publisher.java
package com.demo.eventbus;
import de.greenrobot.event.EventBus;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Publisher extends Activity {
public String txt = "我是Publisher实例";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.publisher);
}
public void toSubscriber(View v)
{
EventBus.getDefault().post(this);
finish();
}
}