How do you pass data between Fragments with Jetpack Navigation?

Type-safe argument flow in Navigation.
Define args in nav graph XML, navigate with generated Directions, read with navArgs() in target Fragment.
Direct Fragment constructors, manual Bundles, or Activity Intent extras.
WHAT THIS TESTS: This question checks whether you rely on type-safe Navigation component patterns rather than legacy Fragment hacks. The interviewer wants to see that you understand declarative argument contracts in the navigation graph, the role of the Safe Args Gradle plugin, and the separation between the origin and destination. It also surfaces whether you know the modern Kotlin property delegate APIs.
A GOOD ANSWER COVERS: A strong answer walks through four steps in order. First, open the navigation graph XML and add an argument element to the destination Fragment with a name like userId and a type like string or integer. Second, ensure the project applies the Safe Args plugin in the build file so the build system generates a Directions class and an action helper. Third, in the origin Fragment, use the generated action class to pass the value and call findNavController().navigate(action) rather than manual transaction code. Fourth, in the receiving Fragment, retrieve the argument with the navArgs() delegate by lazy or from the arguments bundle. Mentioning that Safe Args eliminates string-key typos and provides type safety at build time is a strong plus.
COMMON WRONG ANSWERS: The biggest red flag is suggesting a parameterized Fragment constructor such as new MyFragment(userId) because Fragments must have no-arg constructors for recreation. Another mistake is recommending setTargetFragment or direct Fragment references, which couples the sender and receiver tightly. Some candidates mention Intent extras, which is an Activity pattern, not a Fragment Navigation pattern. Proposing a manual Bundle with string keys without Safe Args also signals outdated or unsafe practices.
LIKELY FOLLOW-UPS: The interviewer may ask what happens if the argument is optional or how to handle complex objects. Be ready to explain that Parcelable or Serializable types can be declared in the graph, or that you can use a custom NavType. They might also ask how to pass data back from the destination to the origin; the modern answer is the Fragment result API or a shared ViewModel scoped to the nav graph, not target fragment.
ONE CONCRETE EXAMPLE: Suppose a list Fragment needs to open a detail Fragment for user ID 42. In nav_graph.xml, the detail destination declares an argument named userId with app:type="integer". After building, the code calls val action = ListFragmentDirections.actionListToDetail(userId = 42) then findNavController().navigate(action). Inside DetailFragment, private val args by navArgs() and args.userId returns 42. If the user backgrounds the app and the process dies, the Navigation component restores the back stack and the argument is preserved automatically.
Source: developer.android.com
Read the original → developer.android.com
- #android
- #jetpack navigation
- #fragments
- #safe args
- #interview
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.