最終更新:2016-02-01 (月) 16:57:09 (1618d)  

android.os.Parcelable
Top / android.os.Parcelable

public interface

Interface for classes whose instances can be written to and restored from a Parcel.

http://developer.android.com/reference/android/os/Parcelable.html

ネストされたクラス

用途

  • 状態の保存と言うとシリアライズを思い浮かべる方もいると思いますがシリアライズは永続化を目的としていますが、Parcelableはプロセス間通信のときなど、一時的に状態を保存したいときに利用します。

SerializableParcelable

Serializable

  • オブジェクトをjava.io.Serializableにすれば,アプリ内を飛ぶIntent上に,オブジェクトを直接乗っける事ができる。
    • Intent.putExtra(key, Serializableなクラス)
    • (Serializableなクラス)Intent.getSerializableExtra?(key)
  • Serializableはその過程でリフレクションが行われるので遅い
  • This method create a lot of temporary objects and cause quite a bit of garbage collection.
  • Serializable interface is easier to implement.

Parcelable

  • オブジェクトをParcelableにすれば,アプリ間をまたぐ事もできる。ただしその分,直列化の処理を自前でコーディングする必要がある。
  • Parcelable process is much faster than serializable. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it.
  • It also stands to reason that the code has been heavily optimized for this purpose.

メソッド

  • abstract intParcelable.describeContents()Describe the kinds of special objects contained in this Parcelable's marshalled representation.
    abstract voidParcelable.writeToParcel?(Parcel dest, int flags)Flatten this object in to a Parcel.
  • public MyObjects(Parcel source) {
        age = source.readInt();
        name = source.readString();
        address = source.createStringArrayList();
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(age);
        dest.writeString(name);
        dest.writeStringList(address);
    }
    public static final Creator<MyObjects> CREATOR = new Creator<MyObjects>() {
        @Override
        public MyObjects[] newArray(int size) {
            return new MyObjects[size];
        }
    
        @Override
        public MyObjects createFromParcel(Parcel source) {
            return new MyObjects(source);
        }
    };

実装

  • Classes implementing the Parcelable interface must also have a non-null static field called CREATOR? of a type that implements the Parcelable.Creator interface.

  • public class MyObjects implements Parcelable {
    
    private int age;
    private String name;
    
    private ArrayList<String> address;
    
    public MyObjects(String name, int age, ArrayList<String> address) {
        this.name = name;
        this.age = age;
        this.address = address;
    
    }
    
    public MyObjects(Parcel source) {
        age = source.readInt();
        name = source.readString();
        address = source.createStringArrayList();
    }
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(age);
        dest.writeString(name);
        dest.writeStringList(address);
    
    }
    
    public int getAge() {
        return age;
    }
    
    public String getName() {
        return name;
    }
    
    public ArrayList<String> getAddress() {
        if (!(address == null))
            return address;
        else
            return new ArrayList<String>();
    }
    
    public static final Creator<MyObjects> CREATOR = new Creator<MyObjects>() {
        @Override
        public MyObjects[] newArray(int size) {
            return new MyObjects[size];
        }
    
        @Override
        public MyObjects createFromParcel(Parcel source) {
            return new MyObjects(source);
        }
    };
    
    }

関連