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:
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:
And the write side uses:
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:
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:
And then:
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.
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
writeByteon write andreadByte() != 0on 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
@Parcelizeremoves most of the manual boilerplate. - Most
Parcelablebugs come from mismatched order or mismatched primitive types.

