How do I pass data between Activities in Android application?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When developing Android applications, efficiently passing data between activities is a fundamental skill. Multiple approaches are available, each with its own use cases and technical considerations. This document will explore these methods, explain their implementations with code examples, and conclude with a comparison table summarizing these techniques.
Intent Extras
Intent is a staple mechanism for passing data between activities in Android. You create an Intent object and attach data using key-value pairs stored as extras. This is suitable for simple data types such as primitives (int, float, double), strings, and other serializable objects.
Example:
Key Considerations:
- Best for small amounts of data.
- Simple and efficient for primitive data types and strings.
- Intent extras must implement the
SerializableorParcelableinterface for complex objects.
Bundles
Bundles are essentially collections of key-value pairs, similar to Intent extras, but with the added advantage of nested data structures. You can pass a Bundle object through an Intent.
Example:
Key Considerations:
- Useful when passing complex data.
- Acts like a lightweight map for organizing properties.
Parcelable
Parcelable is a more efficient alternative to Serializable for complex data due to its ability to serialize data directly into a Parcel. This makes it ideal for passing custom objects between activities.
Example:
Key Considerations:
- Requires more boilerplate than
Serializable. - Must implement the
Parcelableinterface. - Better performance than
Serializable.
Serializable
While not recommended for performance-critical applications, Serializable is a straightforward way to pass objects of classes that implement Serializable. Unlike Parcelable, it uses reflection and is thus slower.
Example:
Key Considerations:
- Simpler as it leverages Java's built-in serialization.
- Can lead to slower performance due to reflection.
Shared Preferences
Not typically intended for data passing between activities, SharedPreferences can be used for persistent storage. Data is stored in key-value pairs and remains accessible across multiple app sessions, making it suitable for small amounts of primitive or string data.
Example:
Key Considerations:
- Best for simple and persistent data storage.
- Not ideal for high-frequency data transfer between activities.
Comparison Table
Here is a comparison of the different data-passing techniques in terms of use-case, complexity, and other factors.
| Method | Use Case | Complexity | Complete Lifecycle | Data Type Suitability | Efficiency |
| Intent Extras | Small, simple data types | Simple | Short/Activity | Primitives, Strings | High |
| Bundles | Nested data types | Moderate | Short/Activity | Complex structures | High |
| Parcelable | Custom class objects | High | Short/Activity | Custom objects | Very High |
| Serializable | Custom class objects | Low | Short/Activity | Custom objects | Low |
| SharedPreferences | Persistent small data | Simple | Persistent/Global | Primitives, Strings | Moderate |
In conclusion, the choice of method for passing data between activities largely depends on the nature of the data, efficiency, and the intended lifecycle of the data. Understanding the strengths and weaknesses of each approach ensures optimal app performance and a cleaner codebase.
Related reading
- How do I pop two views at once from a navigation controller?
- How do I prevent the iPhone screen from dimming or turning off while my application is running?
- How do I print the type or class of a variable in Swift?
- How do I programmatically restart an Android app?
- How do I put the image on the right side of the text in a UIButton?
- How do I record audio on iPhone with AVAudioRecorder?
- How do I remove lines between ListViews on Android?
- How do I remove the blue styling of telephone numbers on iPhone/iOS?
.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.