How to pass an object from one activity to another on Android
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Passing data between activities is a common task in Android development. Although Android provides several ways to pass data between activities, passing complex data structures like objects can be more involved than simply passing primitive data types due to serialization requirements. In this article, we'll explore various methods to pass objects between activities in Android, offering technical explanations and examples to ensure a thorough understanding.
Methods to Pass Objects Between Activities
1. Using Parcelable
The Parcelable interface is particularly optimized for Android. It allows objects to be serialized more efficiently than Serializable. Here's how you can implement it:
Implementation Steps
- Make Your Class Parcelable: Implement the
Parcelableinterface in your object class.
- Pass the Object: Add the Parcelable object to an
Intent.
- Retrieve the Object: Retrieve the Parcable object in the target activity.
2. Using Serializable
While not as efficient as Parcelable, the Serializable interface is easier to implement:
Implementation Steps
- Implement Serializable: Your object class should implement
Serializable.
- Pass the Object: Use
Intentto pass the object to another activity.
- Retrieve the Object: Unwrap the bundled object in the target activity.
3. Using a Static Helper
If you prefer to avoid Parcelable or Serializable, you can use a static helper class, but this approach has its caveat concerning data persistence across configuration changes.
Implementation Steps
- Create a Helper Class: Store a reference to the object.
- Pass the Object: Use the helper class to store the object before starting the activity.
- Retrieve the Object: Fetch the object in the targeted activity.
Comparison Table
| Method | Efficiency | Serialization | Usage Complexity | Ideal Use Case |
Parcelable | High | Yes | Moderate | Passing data between activities efficiently |
Serializable | Low | Yes | Easy | Simplicity and quick implementation |
| Static Helper | Medium | No | Easy | When object persistence across states isn't needed |
Additional Considerations
Choosing Between Parcelable and Serializable
- Performance:
Parcelableis generally faster because it's designed specifically for Android. Use it when performance is crucial. - Ease of Use:
Serializableis simpler to implement, requiring less boilerplate code; therefore, use it for small data structures or when performance isn't an issue.
Handling Parcelable Exceptions
When implementing Parcelable, ensure that all fields are properly written to and read from the Parcel. Missing fields or mismatches can cause runtime crashes.
Conclusion
Passing objects between activities in Android can be achieved using several methods, each with specific advantages and trade-offs. While Parcelable offers high performance, Serializable provides ease of use, and static helpers offer simplicity at the cost of persistence risks. Understanding these methods will allow you to choose the most appropriate solution based on the specific requirements of your application.

