ParcelableSeriaizeable 比较类似, 是序列化的一种方式,不过是Android独有的。

不过与Seriaizeable 不同的是,它并不是持久化的序列化,通常是保存在内存里在通过 IBinder 传播.

官方是这样描述的:

Interface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a non-null static field called CREATOR of a type that implements the Parcelable.Creator interface.

接口的实例可以被写入Parcel, 并且也可以从Parcel里恢复,实现Parcelable的类必须有一个实例化于Parcelable.Creator的叫作CREATOR的非空静态域。

举个栗子:

package com.example.dongyayun.androidartsearch.Parcelable;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

/**
 * Created by dongyayun on 16/1/16.
 */
public class ParcelableTest implements Parcelable {
    public static final Creator<ParcelableTest> CREATOR = new Creator<ParcelableTest>() {

        @Override
        public ParcelableTest createFromParcel(Parcel in) {
            Log.d("ParcelableTest", "Call createFromParcel");
            return new ParcelableTest(in);
        }

        @Override
        public ParcelableTest[] newArray(int size) {
            Log.d("ParcelableTest", "newArray size = " + size);
            return new ParcelableTest[size];
        }
    };
    int t1;
    int t2;
    int t3;
    String a;

    public ParcelableTest(int t1, int t2, int t3, String a) {
        this.t1 = t1;
        this.t2 = t2;
        this.t3 = t3;
        this.a = a;
    }


    protected ParcelableTest(Parcel in) {
        Log.d("ParcelableTest", "Call ParcelableTest");
        t1 = in.readInt();
        t2 = in.readInt();
        t3 = in.readInt();
        a = in.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        Log.d("ParcelableTest", "Call writeToParcel");
        dest.writeInt(t1);
        dest.writeInt(t2);
        dest.writeInt(t3);
        dest.writeString(a);
    }

    @Override
    public String toString() {
        Log.d("ParcelableTest", "t1 = " + t1 + " t2 = " + t2 + " t3 = " + t3 + " a = " + a);
        return super.toString();
    }
}

上面是实例化了 Parcelable 的一个类,在每个方法里加了Log。

接下来就用Android里常用的通信场景来体验一下Parcelable

private void testMyParcelableTest() {
    ParcelableTest parcelableTest = new ParcelableTest(1, 2, 3, "HelloWorld!");

    parcelableTest.toString();

    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("key", parcelableTest);

    startActivity(intent);
}

import com.example.dongyayun.androidartsearch.Parcelable.ParcelableTest;

/**
 * Created by dongyayun on 16/1/16.
 */
public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();

        ParcelableTest bundle = (ParcelableTest) intent.getParcelableExtra("key");

        bundle.toString();

    }
}

这个例子表示的是 MainActiviySecondActivity 之间通过Parcelable 来进行通信。

来看一下调用 Activity 通信时Parcelable发生了哪些事情。

D/ParcelableTest( 2293): t1 = 1 t2 = 2 t3 = 3 a = HelloWorld!
D/ParcelableTest( 2293): Call writeToParcel
D/ParcelableTest( 2293): Call createFromParcel
D/ParcelableTest( 2293): Call ParcelableTest
D/ParcelableTest( 2293): t1 = 1 t2 = 2 t3 = 3 a = HelloWorld!

首先是调用writeToParcel 将数据写入 Parcel 中,再SecondActivity 启动时,调用createFromParcelParcel里恢复数据。

另外使用有一个 AndroidStudio 插件可以将类一键 Parcelableandroid-parcelable-intellij-plugin

ParcelableSeriaizeable 的速度要快很多,这里有比较

所以如果仅需要进程间的通讯,建议使用Parcelable!