What's the best way to share data between activities?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
There is no single best mechanism for sharing data between Android activities. The right choice depends on how much data you need to move, whether the data should survive process death, and whether the second activity is just a detail screen or part of a larger workflow.
For most navigation flows, Intent extras are the correct default. When the data is large, shared by many screens, or needs to outlive an activity instance, move the data into a repository, database, or other persistent store and pass only an identifier.
Use Intent Extras for Small, Immediate Data
If one activity launches another and the target only needs a few values, use extras on the Intent. This is simple, explicit, and works well with Android lifecycle rules.
Primitive values and strings are straightforward:
This approach is best when the receiving activity can render itself entirely from the values in the extra bundle. It is also easy to test because the contract is visible at the call site.
Prefer Parcelable for Structured Objects
When you need to pass a small structured object, use Parcelable rather than Java serialization. Parcelable is the Android-native option and avoids much of the cost of reflective serialization.
Keep payloads small. Passing a deeply nested object graph, a bitmap, or a large list through extras can trigger transaction size problems and makes activity launches brittle.
Pass Identifiers for Large or Shared Data
If the second activity needs a full domain object, send only the stable key and reload the data from a repository. This keeps navigation lightweight and avoids stale copies of mutable state.
This pattern is usually the most maintainable when multiple screens can update the same record. Instead of copying state from activity to activity, every screen reads from the same source of truth.
Returning Data Back to the Caller
If activity B needs to send a result back to activity A, use the Activity Result API instead of older callback patterns.
This keeps the request and response flow explicit and lifecycle-aware.
When to Use Shared Storage Instead
Not all cross-screen data should travel through intents. Use shared storage when the data should be available after process death, app restart, or across many destinations:
- '
Roomor another database for durable business data' - '
DataStoreorSharedPreferencesfor small settings' - A repository layer for shared cached objects
Passing a database key through the intent and reading the latest value from storage is often the cleanest design.
Common Pitfalls
- Sending large objects in extras can exceed binder transaction limits. Pass an ID instead.
- Using
Serializablefor convenience works, but it is usually slower and less idiomatic on Android thanParcelable. - Depending on a singleton for screen-to-screen state often creates hidden coupling and stale data problems.
- Forgetting null handling for missing extras can crash the receiving activity. Validate inputs early.
- Using hard-coded string keys in many places is error-prone. Centralize them in constants or helper methods.
Summary
- '
Intentextras are the best default for small, immediate navigation data.' - Use
Parcelablefor compact structured payloads that genuinely need to cross activity boundaries. - For large or shared state, pass an identifier and load from a repository or database.
- Use the Activity Result API when the launched activity must return data.
- Avoid singletons and oversized extras because they create fragile activity contracts.
Related reading
- What's the cleanest way of applying map to a dictionary in Swift?
- What's the difference between a protocol extended from AnyObject and a class-only protocol?
- What's the difference between Architectures and Valid Architectures in Xcode Build Settings?
- What's the difference between commit and apply in SharedPreferences
- What's the difference between fill_parent and wrap_content?
- What's the difference between setWebViewClient vs. setWebChromeClient?
- What's the difference between src/androidtest and src/test folders?
- What's the difference between the various methods to get an Android Context?
.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.