Android development
enums
object passing
intents
mobile programming

Passing enum or object through an intent the best solution

Master System Design with Codemia

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

Introduction

The best way to pass data through an Android Intent depends on what the data actually is. For enums, a stable string or dedicated code is usually enough. For small app-internal objects, Parcelable is the normal Android-friendly choice. For large or important objects, passing only an ID and reloading the data in the destination is usually safer.

Pass Enums as Stable Values

Enums are small and finite, so they do not need heavyweight serialization. The safest default is to pass the enum name rather than its ordinal:

kotlin
1enum class UserStatus {
2    ACTIVE,
3    BLOCKED,
4    PENDING
5}
6
7val intent = Intent(this, DetailActivity::class.java)
8intent.putExtra("status", UserStatus.ACTIVE.name)
9startActivity(intent)

Then read it back:

kotlin
val statusName = intent.getStringExtra("status") ?: UserStatus.PENDING.name
val status = UserStatus.valueOf(statusName)

Why use name instead of ordinal? Because ordinals change if you reorder or insert enum constants. Names are far more stable for cross-activity communication.

Use Parcelable for Small Custom Objects

For custom objects that really should travel with the Intent, Parcelable is the Android-native option. In Kotlin, @Parcelize makes this very practical:

kotlin
1import android.os.Parcelable
2import kotlinx.parcelize.Parcelize
3
4@Parcelize
5data class UserSummary(
6    val id: Long,
7    val displayName: String,
8    val status: String
9) : Parcelable

Put it into the intent:

kotlin
1val user = UserSummary(42L, "Alice", "ACTIVE")
2
3val intent = Intent(this, DetailActivity::class.java)
4intent.putExtra("user_summary", user)
5startActivity(intent)

And receive it:

kotlin
val user = intent.getParcelableExtra<UserSummary>("user_summary")

This is usually the best answer for small, self-contained transfer objects.

Pass IDs for Large or Important Domain Objects

Even though Parcelable works, it is often the wrong choice for large domain objects. If the target screen can load the data again from a repository, database, or network layer, pass only the identifier:

kotlin
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra("user_id", 42L)
startActivity(intent)

Then in the destination:

kotlin
val userId = intent.getLongExtra("user_id", -1L)

This pattern is more resilient because:

  • the intent payload stays small
  • the destination gets fresh data
  • process recreation is easier to reason about
  • version mismatches between activities matter less

For anything that already has a durable identity, passing the ID is often cleaner than serializing the whole object.

Why Serializable Is Usually Not the Best Default

Java Serializable can work, but it is usually not the best Android default for intent extras. It is more generic, often slower, and less Android-specific than Parcelable.

That does not mean it is forbidden. It means that if you are designing an Android app boundary intentionally, Parcelable or ID passing is usually the better fit.

Keep Intent Payloads Small

It is tempting to use intents as a generic object transport layer. That becomes risky quickly. Large payloads can cause binder transaction issues, make process recreation harder, and create stale-data problems between screens.

As a rule:

  • enum: pass a stable name or code
  • small DTO: Parcelable
  • large entity: pass an ID

That simple rule covers most real applications well.

Common Pitfalls

The most common mistake is passing enum ordinals. They are compact, but not stable under refactoring.

Another pitfall is sending large complex objects through the intent when the destination could just load them from a shared source using an ID. That creates bigger payloads and more fragile screen boundaries.

It is also easy to overuse Serializable because it looks simple at first. In Android, Parcelable is usually the more intentional choice for small transport objects.

Finally, remember that intent extras are not a substitute for a proper data layer. If the object is authoritative application state, the destination should often reload it rather than trust a stale serialized copy.

Summary

  • Pass enums as stable names or codes, not ordinals.
  • Use Parcelable for small Android transfer objects.
  • Prefer passing IDs for large or important domain objects.
  • Keep intent extras small and purpose-built.
  • Choose the transport format based on data stability and payload size, not just on what is easiest to serialize.

Course illustration
Course illustration

All Rights Reserved.