Parcelable in Android : Java and Kotlin

Abhinay Gupta
4 min readJun 11, 2023

In Android, the `Parcelable` interface is used to serialize and deserialize custom objects so that they can be passed between components, such as activities, fragments, and services. By implementing the `Parcelable` interface, you define how your object can be serialized into a `Parcel` and reconstructed back from it.

The `Parcel` class is an optimized container for transmitting data across Android components. It provides methods to write and read data of various types, including primitive types, arrays, and objects.

To make an object parcelable, you need to follow these steps:

  1. Implement the `Parcelable` interface:
public class MyObject implements Parcelable {
// Class implementation
}

2. Implement the `describeContents()` method:

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

The `describeContents()` method should typically return 0, unless you have special requirements for the contents of the `Parcelable`.

3. Implement the `writeToParcel()` method:

@Override
public void writeToParcel(Parcel dest, int flags) {
// Write object properties to the parcel
}

In the `writeToParcel()` method, you need to write the object properties to the `Parcel` using the appropriate methods like `writeInt()`, `writeString()`, or `writeParcelable()`.

4. Implement a `CREATOR` field:

public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() {
@Override
public MyObject createFromParcel(Parcel in) {
return new MyObject(in);
}
@Override
public MyObject[] newArray(int size) {
return new MyObject[size];
}
};

The `CREATOR` field is a special constant that represents a `Parcelable.Creator` implementation for your class. It provides methods to create a new instance of the `Parcelable` class from a `Parcel` (`createFromParcel()`) and create an array of the `Parcelable` class (`newArray()`).

5. Implement a constructor that takes a `Parcel` as an input:

protected MyObject(Parcel in) {
// Read object properties from the parcel
}

In the constructor, you need to read the object properties from the `Parcel` using the corresponding methods like `readInt()`, `readString()`, or `readParcelable()`.

With these steps implemented, your custom object is now parcelable and can be passed between components using `Intents` or other mechanisms that rely on `Parcelable` data.

Here’s an example of a `Person` class implementing the `Parcelable` interface:

public class Person implements Parcelable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
protected Person(Parcel in) {
name = in.readString();
age = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
}

With the `Parcelable` implementation in place, you can now pass instances of `Person` between components using `Intents` or other methods that support `Parcelable` objects.

Internally, the `Parcelable` mechanism in Android works by serializing and deserializing objects into a binary format using the `Parcel` class.

When you call `writeToParcel()` on a `Parcelable` object, the system creates a new `Parcel` object and starts writing the object’s data to it. The `Parcel` class provides various methods to write different types of data, such as `writeInt()`, `writeString()`, or `writeParcelable()`. It also handles the necessary conversions and encoding to represent the data in a compact binary format.

On the receiving side, when you pass a `Parcelable` object from one component to another, such as when sending it via an `Intent`, the system reads the binary data from the `Parcel` using the corresponding `read` methods like `readInt()`, `readString()`, or `readParcelable()`. It then reconstructs the original object by using the retrieved data.

The `Parcel` class is optimized for performance and efficiency in serialization and deserialization. It uses a compact binary format, which reduces the size of the data being transmitted and improves efficiency in reading and writing data.

The `Parcelable` interface is designed specifically for Android, and its implementation is lightweight compared to other serialization mechanisms like `Serializable`. By using `Parcelable`, you have more control over the serialization process and can optimize it for your specific use case.

Overall, the `Parcelable` mechanism in Android provides a fast and efficient way to serialize and deserialize objects, making them suitable for inter-component communication and persistence.

In Kotlin, the `Parcelable` mechanism works in a similar way as in Java. However, Kotlin provides some additional features and syntactic sugar that make implementing `Parcelable` classes more concise.

To make a class `Parcelable` in Kotlin, you need to follow these steps:

  1. Annotate the class with `@Parcelize`:
@Parcelize
class Person(val name: String, val age: Int) : Parcelable

The `@Parcelize` annotation is provided by the `kotlinx.android.parcel` plugin, which is automatically applied when you include the `androidx.lifecycle:lifecycle-extensions` dependency in your project. It eliminates the need to implement the `Parcelable` interface manually.

2. Ensure the class inherits from `Parcelable`:

class Person(val name: String, val age: Int) : Parcelable

Make sure the class implements the `Parcelable` interface, as required by the `@Parcelize` annotation.

That’s it! With just these two steps, your Kotlin class is now `Parcelable`.

Under the hood, the Kotlin compiler generates the necessary `writeToParcel()` and `createFromParcel()` methods for you. It automatically handles the serialization and deserialization process based on the properties defined in your class. This reduces boilerplate code and simplifies the implementation of `Parcelable` classes.

Here’s an example of using a `Parcelable` class in Kotlin:

@Parcelize
data class Person(val name: String, val age: Int) : Parcelable
// Usage:
val person = Person("John Doe", 25)
// Serialize the object into a byte array
val parcel = Parcel.obtain()
person.writeToParcel(parcel, 0)
val byteArray = parcel.marshall()
// Deserialize the byte array back into an object
parcel.unmarshall(byteArray, 0, byteArray.size)
parcel.setDataPosition(0)
val deserializedPerson = Person.CREATOR.createFromParcel(parcel)

In Kotlin, you can directly use the `writeToParcel()` and `createFromParcel()` methods provided by the compiler. The `@Parcelize` annotation takes care of generating these methods and handling the serialization and deserialization process for you.

Note that the `@Parcelize` annotation is only available for classes that meet certain requirements, such as having a primary constructor and all properties being `Parcelable` themselves. If your class has complex properties or custom serialization logic, you may need to implement the `Parcelable` interface manually.

Overall, Kotlin simplifies the process of making classes `Parcelable` by providing the `@Parcelize` annotation and automatic generation of the required methods, reducing boilerplate code and making the implementation more concise.

--

--