How do you share a ViewModel between Fragments?

Tests your understanding of ViewModel lifecycle scoping beyond a single screen. To share, scope the ViewModel to the parent Activity or a navigation graph using activityViewModels() or navGraphViewModels().
What's really being asked
This question assesses your practical knowledge of the Android Jetpack ViewModel component and its role in modern app architecture. The interviewer is looking for more than just the basic by viewModels() delegate. They want to see if you understand how to correctly scope a ViewModel's lifecycle to a component that outlives a single Fragment, such as a parent Activity or a navigation graph. This demonstrates your ability to manage shared state and handle inter-fragment communication in a clean, lifecycle-aware, and testable way.
The full answer
A strong answer outlines a clear, modern approach. First, identify the shared lifecycle owner for the fragments, which is typically the host Activity or a navigation graph if using the Navigation component. Second, explain the mechanism: use the activityViewModels() KTX delegate in each fragment to retrieve a ViewModel instance scoped to the Activity. Alternatively, and preferably for more granular control, use navGraphViewModels(R.id.my_nav_graph) to scope the ViewModel to a specific subgraph. Third, describe the communication flow: one fragment updates data in the shared ViewModel (e.g., a MutableStateFlow), and other fragments observe that data to react to changes. Finally, state the key benefit: this decouples the fragments, as they only need to know about the ViewModel.
The mistakes people make
A major red flag is suggesting non-lifecycle-aware or outdated patterns. This includes creating a singleton object to hold state (which can cause memory leaks), using setTargetFragment (which is deprecated and creates tight coupling), or defining interfaces on the host Activity that fragments must call. While the interface pattern works, it's boilerplate-heavy and less scalable than a shared ViewModel. A weak answer just says "use a shared ViewModel" without specifying the scoping mechanism (activityViewModels or navGraphViewModels) or explaining how it works.
What usually comes next
Be prepared for questions about the risks. For example, "What's the risk of scoping to the Activity?" The ViewModel might live longer than necessary, holding onto memory if only a few fragments in a deep navigation stack need it. This is why navGraphViewModels is often better. Another follow-up could be, "How would you handle dependency injection for this shared ViewModel using Hilt?" The answer is to use @HiltViewModel and inject it normally; Hilt respects the scoping requested by the KTX delegate.
A concrete example
In a master-detail flow, a ProductListFragment and a CartFragment need to share cart state. Create a CartViewModel scoped to the parent Activity. In each fragment, get the instance via private val cartViewModel: CartViewModel by activityViewModels(). When a user adds an item in ProductListFragment, it calls cartViewModel.addItem(product). The CartFragment observes cartViewModel.cartItems (a StateFlow) and automatically updates its UI when the list changes, without ever directly communicating with the ProductListFragment.
Interview question
In a multi-fragment checkout flow, what is the recommended, lifecycle-aware approach for managing a shared shopping cart state across all fragments?
- a.Instantiate a ViewModel using `activityViewModels()` or `navGraphViewModels()` in each fragment to access a shared, scoped instance.Correct
- b.Create a singleton object to hold the cart state, making it globally accessible to all fragments.
- c.Define a communication interface in the host Activity and have each fragment cast its context to that interface to pass data.
- d.Use the standard `viewModels()` delegate in each fragment and pass data between them using `FragmentResultListener`.
Why? this is the answer
Scoping a ViewModel to a shared lifecycle owner (the Activity or a NavGraph) creates a single, lifecycle-aware source of truth. While `FragmentResultListener` is a modern API, it's better for one-time events, not for managing continuous shared state like a shopping cart.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles