Parcelable
Boolean
Android Development
Serialization
Java Programming

How to read/write a boolean when implementing the Parcelable interface?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When implementing Parcelable, booleans are usually written as a byte and read back by checking whether that byte is zero. That pattern became standard because older Android APIs did not have direct boolean parcel methods, and it remains the most compatible approach across Android versions.

The Usual Parcelable Pattern for a Boolean

A Parcel stores primitive values in sequence, so your writeToParcel method and parcel constructor must read and write fields in exactly the same order.

For a boolean, the classic pattern looks like this:

java
1public class UserSettings implements Parcelable {
2    private final boolean notificationsEnabled;
3
4    public UserSettings(boolean notificationsEnabled) {
5        this.notificationsEnabled = notificationsEnabled;
6    }
7
8    protected UserSettings(Parcel in) {
9        notificationsEnabled = in.readByte() != 0;
10    }
11
12    @Override
13    public void writeToParcel(Parcel dest, int flags) {
14        dest.writeByte((byte) (notificationsEnabled ? 1 : 0));
15    }
16
17    @Override
18    public int describeContents() {
19        return 0;
20    }
21
22    public static final Creator<UserSettings> CREATOR = new Creator<UserSettings>() {
23        @Override
24        public UserSettings createFromParcel(Parcel in) {
25            return new UserSettings(in);
26        }
27
28        @Override
29        public UserSettings[] newArray(int size) {
30            return new UserSettings[size];
31        }
32    };
33}

This is the most portable answer because it works well on older and newer Android releases.

Why a Byte Is Commonly Used

Parcel historically did not expose direct boolean helpers in the way many developers expected, so developers encoded true as 1 and false as 0.

The read side uses:

java
boolean value = in.readByte() != 0;

And the write side uses:

java
dest.writeByte((byte) (value ? 1 : 0));

That pattern is simple, explicit, and easy to audit when you later add more fields.

Keep the Read and Write Order Identical

The single most important rule of Parcelable is order. If you write fields in one order and read them back in another, the object is corrupted.

For example:

java
1@Override
2public void writeToParcel(Parcel dest, int flags) {
3    dest.writeString(name);
4    dest.writeByte((byte) (notificationsEnabled ? 1 : 0));
5}
6
7protected UserSettings(Parcel in) {
8    name = in.readString();
9    notificationsEnabled = in.readByte() != 0;
10}

That is correct because the constructor reads in the same order the values were written.

If the read order changes accidentally, the boolean may be read from the wrong position and you get hard-to-debug runtime bugs.

Newer APIs and Alternative Helpers

On newer Android API levels, Parcel also has direct boolean helpers. Even so, many projects still prefer the byte pattern because it works across a wider compatibility range and matches older codebases.

If your app’s minimum SDK and code style allow it, the direct API can look cleaner:

java
dest.writeBoolean(notificationsEnabled);

And then:

java
notificationsEnabled = in.readBoolean();

The tradeoff is compatibility with older APIs and consistency with the rest of the project.

Kotlin and @Parcelize

If you are writing Kotlin, manual Parcelable code is often replaced with @Parcelize, which generates the boilerplate for you.

kotlin
1@Parcelize
2data class UserSettings(
3    val notificationsEnabled: Boolean
4) : Parcelable

That is convenient, but the underlying idea is still the same: the value must be serialized and deserialized consistently.

Common Pitfalls

The most common mistake is reading fields in a different order than they were written. With Parcelable, order is part of the contract.

Another pitfall is mixing different primitive read and write methods. If you write a byte and later try to read an int, the parcel data is no longer interpreted correctly.

Developers also sometimes forget that Parcelable is version-sensitive. If you add new fields later, you need to update both writing and reading logic together.

Finally, if you can avoid manual parcel code in Kotlin with @Parcelize, do so. The generated code reduces a lot of repetitive mistakes.

Summary

  • The most compatible way to parcel a boolean is writeByte on write and readByte() != 0 on read.
  • Read and write parcel fields in exactly the same order.
  • Direct boolean parcel methods exist on newer APIs, but the byte pattern remains widely used.
  • Kotlin @Parcelize removes most of the manual boilerplate.
  • Most Parcelable bugs come from mismatched order or mismatched primitive types.

Course illustration
Course illustration

All Rights Reserved.