Parcelable


public interface Parcelable

android.os.Parcelable


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 public static field called CREATOR of a type that implements the Parcelable.Creator interface.

A typical implementation of Parcelable is:

Kotlin

 class MyParcelable private constructor(`in`: Parcel) : Parcelable {
     private val mData: Int = `in`.readInt()

     override fun describeContents(): Int {
         return 0
     }

     override fun writeToParcel(out: Parcel, flags: Int) {
         out.writeInt(mData)
     }

     companion object CREATOR: Parcelable.Creator<MyParcelable?> {
         override fun createFromParcel(`in`: Parcel): MyParcelable? {
             return MyParcelable(`in`)
         }

         override fun newArray(size: Int): Array<MyParcelable?> {
             return arrayOfNulls(size)
         }
     }
 }
 

Java

 public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

     public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }

         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];
         }
     };

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 }

Summary

Nested classes

interface Parcelable.ClassLoaderCreator<T>

Specialization of Creator that allows you to receive the ClassLoader the object is being created in. 

interface Parcelable.Creator<T>

Interface that must be implemented and provided as a public CREATOR field that generates instances of your Parcelable class from a Parcel. 

Constants

int CONTENTS_FILE_DESCRIPTOR

Descriptor bit used with describeContents(): indicates that the Parcelable object's flattened representation includes a file descriptor.

int PARCELABLE_WRITE_RETURN_VALUE

Flag for use with writeToParcel(Parcel, int): the object being written is a return value, that is the result of a function such as "Parcelable someFunction()", "void someFunction(out Parcelable)", or "void someFunction(inout Parcelable)".

Public methods

abstract int describeContents()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.

abstract void writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

Constants

CONTENTS_FILE_DESCRIPTOR

Added in API level 1
public static final int CONTENTS_FILE_DESCRIPTOR

Descriptor bit used with describeContents(): indicates that the Parcelable object's flattened representation includes a file descriptor.

See also:

Constant Value: 1 (0x00000001)

PARCELABLE_WRITE_RETURN_VALUE

Added in API level 1
public static final int PARCELABLE_WRITE_RETURN_VALUE

Flag for use with writeToParcel(Parcel, int): the object being written is a return value, that is the result of a function such as "Parcelable someFunction()", "void someFunction(out Parcelable)", or "void someFunction(inout Parcelable)". Some implementations may want to release resources at this point.

Constant Value: 1 (0x00000001)

Public methods

describeContents

Added in API level 1
public abstract int describeContents ()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(android.os.Parcel, int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit.

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or CONTENTS_FILE_DESCRIPTOR

writeToParcel

Added in API level 1
public abstract void writeToParcel (Parcel dest, 
                int flags)

Flatten this object in to a Parcel.

Parameters
dest Parcel: The Parcel in which the object should be written. This value cannot be null.

flags int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES