Passing data between a fragment and its container activity
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Fragments are reusable UI components that live inside an Activity. Because they are designed to be modular, they should not reference their host Activity directly. Instead, Android provides several well-defined patterns for sending data back and forth between a Fragment and its container Activity. Choosing the right pattern depends on the complexity of the data and the architecture of your app.
Bundle Arguments
The simplest way to pass data into a Fragment is through a Bundle attached as arguments. This is the standard pattern for initial configuration data.
Bundle arguments survive configuration changes (such as rotation) because the system saves and restores them automatically. Use this approach for simple, serializable values that the Fragment needs at creation time.
Shared ViewModel
A ViewModel scoped to the Activity can be observed by both the Activity and any of its Fragments. This is the recommended approach for sharing dynamic, observable data.
Because the ViewModel is scoped to the Activity, any Fragment that calls activityViewModels() gets the same instance. The data survives configuration changes and the Fragment never holds a direct reference to the Activity.
Interface Callbacks
Before ViewModels existed, the standard pattern was to define an interface in the Fragment and have the Activity implement it. You may still encounter this in older codebases.
This pattern works, but it tightly couples the Fragment to any Activity that hosts it and requires manual null-clearing in onDetach to avoid memory leaks.
Fragment Result API
The Fragment Result API was introduced in AndroidX Fragment 1.3.0 as a type-safe, lifecycle-aware replacement for interface callbacks. The Fragment sends a result through the FragmentManager, and the listener receives it only when the host is in the STARTED state or higher.
The key advantage is that neither side needs to know about the other. The Fragment only needs the request key string, and the Activity registers a listener for that same key.
Navigation Safe Args
If you use the Jetpack Navigation component, Safe Args generates type-safe classes for passing data between destinations. This eliminates raw string keys and catches type mismatches at compile time.
First, add the Safe Args plugin:
Define arguments in your navigation graph XML:
Navigate with type-safe arguments:
Safe Args is the best option when your navigation is already managed by the Navigation component because it provides compile-time safety and eliminates boilerplate Bundle handling.
Common Pitfalls
- Calling
getActivity()and casting it directly without a null check, which causes a crash if the Fragment is detached. - Forgetting to set the Fragment's arguments before the transaction commits, so
getArguments()returns null insideonViewCreated. - Using a Fragment-scoped ViewModel (
by viewModels()) instead of an Activity-scoped one (by activityViewModels()), which creates a separate ViewModel instance that the Activity cannot observe. - Not clearing the interface callback reference in
onDetach, leading to a memory leak that keeps the old Activity alive after a configuration change. - Using raw string keys for Bundle extras across multiple files without constants, which leads to silent key mismatches that are hard to debug.
Summary
- Use Bundle arguments for simple, one-time data that a Fragment needs at creation.
- Use a shared ViewModel scoped to the Activity for dynamic, observable data that survives configuration changes.
- Interface callbacks work but create tight coupling; prefer the Fragment Result API for decoupled communication.
- The Fragment Result API is lifecycle-aware and does not require the Fragment to know anything about its host.
- Navigation Safe Args provide compile-time type safety and are the best choice when you already use the Jetpack Navigation component.
Related reading
- Passing JVM args to Docker image of Spring boot app on Kubernetes
- Passing long configuration file to Kubernetes
- Passing NODE_ENV to docker to run package.json scripts
- Permission denied on accessing host directory in Docker
- Passing data between view controllers
- Passing data between view controllers
- Pod in Kubernetes always in pending state
- Pod status as CreateContainerConfigError in Minikube cluster

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.