How to transfer some data to another Fragment?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Passing data from one Android Fragment to another is a common task, but the right solution depends on what kind of data you are moving. If the destination fragment needs initial input when it is created, use fragment arguments. If both fragments need to observe shared changing state, use a shared ViewModel.
Trying to pass data through fragment constructors or by reaching directly into another fragment instance usually creates lifecycle bugs. Android gives you better tools, and using the right one makes fragment communication predictable.
Use Fragment Arguments for Initial Input
The safest pattern is to package the required values into arguments before the destination fragment is attached.
And from the sending fragment:
This works well because arguments are lifecycle-aware. Android can recreate the fragment after configuration changes and still restore the same input bundle.
Use a Shared ViewModel for Shared State
If the data changes over time and both fragments need to observe it, arguments are not the best tool. A shared ViewModel scoped to the parent activity is usually cleaner.
Sender fragment:
Receiver fragment:
This pattern is ideal when the destination fragment is already on screen or when multiple fragments must react to the same selection.
Which Pattern Should You Choose
A simple rule works well:
- Use arguments for one-time navigation input.
- Use a shared
ViewModelfor ongoing shared state. - Use the Fragment Result API when a child fragment should return a result back to a previous fragment.
If you use the Navigation component, Safe Args gives you a typed version of the arguments pattern. The underlying idea stays the same: treat navigation input as explicit state passed through the fragment manager, not as a direct object reference.
Keep the Payload Small
Passing large objects between fragments is a bad habit. Prefer sending an ID, key, or lightweight value and loading the full object from a repository or ViewModel.
For example, pass userId = "42" rather than serializing an entire profile object. That keeps bundles small and makes process recreation much safer.
Common Pitfalls
- Using a custom fragment constructor. Android may recreate the fragment later without calling your custom constructor the way you expect.
- Writing directly to fields on another fragment instance. That breaks as soon as the destination is recreated.
- Passing large or complex objects in a
Bundle. Binder transaction size limits and lifecycle issues can appear quickly. - Observing shared state with the wrong lifecycle owner. Use
viewLifecycleOwnerinside fragments to avoid leaks and stale observers. - Using arguments for data that is supposed to keep changing after navigation.
Summary
- Fragment arguments are the default choice for passing input into a new fragment.
- A shared
ViewModelis better when multiple fragments need to observe the same changing data. - Avoid custom constructors and direct fragment-to-fragment references.
- Pass small stable identifiers instead of large serialized objects.
- Choosing the right communication pattern upfront prevents most fragment lifecycle bugs.
Related reading
- How to trap on UIViewAlertForUnsatisfiableConstraints?
- How to turn on front flash light programmatically in Android?
- How to underline a UILabel in swift?
- How to underline a UILabel in swift?
- How to uninstall downloaded Xcode simulator?
- How to update Gradle in Android Studio?
- How to update RecyclerView Adapter Data
- How to update the constant height constraint of a UIView programmatically?
.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.