EventBus3.0的跳转传值

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_34123799/article/details/64443077

EventBus3.0的跳转传值

在项目中有时候两个Activity跳转或者Activity回传值可以用intent携带基本类型(或对象),基本类型传值没什么问题,但是传对象的时候需要这个对象实现Serializable或者Parcelable接口,但是,有时候比如项目中咱们自己定义了一个类(这个类里面使用了好多的其他自定义类),如果是这种类型的话用intent传值需要把这个类里面的使用的所有类都序列化,这样的改动太大了,这时候我就想到了用EventBus3.0。


需求说明:

现在有两个Activity,A和B,点A中一个按钮跳转到B中并且传递一个对象,B中接收对象

  1. 第一步:在build.gradle中添加compile 'org.greenrobot:eventbus:3.0.0'点击添加完成点击Sync New,等待下载完成即可。

  2. 第二步:创建一个eventvus传递类。

    /**
     * eventvus传递类
     *
     * Created by YJH on 2017/3/21 15:05.
     */
    public class Person {
    
        private int age;
        private String name;
        private String address;
    
        public Person(int age, String name, String address) {
            this.age = age;
            this.name = name;
            this.address = address;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
         @Override
        public String toString() {
        return  "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
        }
    }
    

3.第三步:创建AActivity

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_a"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.my.eventbusdemo.AActivity">

    <Button
        android:onClick="btnClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到BActivity"
        android:textAllCaps="false" />
</RelativeLayout>

代码:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import org.greenrobot.eventbus.EventBus;

public class AActivity extends AppCompatActivity {

    private Person person;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        person = new Person(10, "王健林", "中国");
    }

    public void btnClick(View view) {
        //发送粘性事件
        EventBus.getDefault().postSticky(person);
        startActivity(new Intent(this, BActivity.class));
    }
}
sticky 粘性事件

关于粘性事件,可以参考Android的广播机制,其中有一个粘性广播,粘性广播的意思是:该广播发送后,会保存在内存中,如果后来有注册的Receiver与之匹配,那么该Receiver便会接收到该广播。那么粘性事件同理,在注册之前便把事件发生出去,等到注册之后便会收到最近发送的粘性事件(必须匹配)。注意:只会接收到最近发送的一次粘性事件,之前的会接受不到。

4.创建BActivity

布局代码:

<?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:orientation="vertical">

    <TextView
        android:id="@+id/tv_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

代码:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by YJH on 2017/3/21 15:03.
 */

public class BActivity extends AppCompatActivity {

    private TextView tv_b;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);

        tv_b = (TextView) findViewById(R.id.tv_b);
        EventBus.getDefault().register(this);
    }

   /**
    * EventBus的接收方法
    *
    * @param person 传递类
    */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void eventBusReceive(Person person) {
        tv_b.setText(person.toString());
    }

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

5.最后不要忘记在AndroidManifest中注册

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".AActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".BActivity"/>

</application>

这样就可以实现自定义类不序列化就传对象了,但切记传递对象最好使用intent方法,用EventBus肯定会对性能有所降低的!!!

猜你喜欢

转载自blog.csdn.net/github_34123799/article/details/64443077
今日推荐