Added in API level 1

Parcelable


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
abstract

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

abstract

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

Constants
static Int

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

static Int

Flag for use with writeToParcel: 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

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

abstract Unit
writeToParcel(dest: Parcel, flags: Int)

Flatten this object in to a Parcel.

Constants

CONTENTS_FILE_DESCRIPTOR

Added in API level 1
static val CONTENTS_FILE_DESCRIPTOR: Int

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

Value: 1

PARCELABLE_WRITE_RETURN_VALUE

Added in API level 1
static val PARCELABLE_WRITE_RETURN_VALUE: Int

Flag for use with writeToParcel: 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.

Value: 1

Public methods

describeContents

Added in API level 1
abstract fun describeContents(): Int

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.

Return
Int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or android.os.Parcelable#CONTENTS_FILE_DESCRIPTOR

writeToParcel

Added in API level 1
abstract fun writeToParcel(
    dest: Parcel,
    flags: Int
): Unit

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 android.os.Parcelable#PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES