How to pass an object from one activity to another on Android
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Passing an object from one Android activity to another usually means putting that object into an Intent extra. Primitive values are easy, but custom objects need a serialization mechanism that Android can place into a Bundle and reconstruct in the destination activity.
Prefer Parcelable for Android Objects
Parcelable is the Android-native approach and is generally preferred over Serializable for app components because it is designed for Bundle and Intent transport.
Here is a simple Java model class:
Once the class implements Parcelable, you can send it through the intent that launches the next activity.
Sending the Object
In the source activity:
The key "user_extra" is just a string identifier. In a larger codebase, define such keys as constants to avoid typos.
Receiving the Object
In the destination activity:
That is the full round trip. The object is written into the intent extras, then rebuilt in the second activity.
What About Serializable
Serializable also works and takes less code:
You would then use putExtra and getSerializableExtra. That approach is simpler for quick prototypes, but Android developers usually choose Parcelable for component-to-component transport because it is the platform-friendly option.
Keep the Payload Small
Passing an object through an intent is best for small, self-contained data. If the object contains large images, long lists, or database-sized payloads, pass an identifier instead and reload the real data in the next activity.
For example, send a user ID and let the destination activity fetch the full record from a repository or ViewModel. This is more robust across process death and configuration changes.
Common Pitfalls
The most common bug is forgetting to implement the parcel read and write logic in the same order. If you write name first and age second, you must read them back in that exact sequence.
Another issue is using different keys in the sending and receiving activities. "user_extra" and "user" are not the same key, and the destination will receive null.
A third mistake is passing very large objects. Intents and bundles are not meant to carry heavy application state, and oversized payloads can cause crashes or lifecycle problems.
Finally, avoid using singletons as a hidden transport channel between activities. They can appear to work at first, but they do not survive process recreation reliably and often make state harder to reason about.
Summary
- The common Android solution is to pass custom objects through an
Intentextra. - '
Parcelableis generally preferred overSerializablefor Android component communication.' - Write and read parcel fields in the same order.
- Use a shared constant for the extra key to avoid typos.
- Pass lightweight objects only; for large state, send an ID and reload the data in the destination activity.
Related reading
- How to pass an object from one activity to another on Android
- How to pass data from 2nd activity to 1st activity when pressed back? - android
- How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0?
- How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in Swift 2.0?
- How to pass EnvironmentObject into View Model in SwiftUI?
- How to pass object with NSNotificationCenter
- How to pass one SwiftUI View as a variable to another View struct
- How to pass prepareForSegue an object
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.