Skip to content
tezvyn:

How do you pass data between Fragments with Jetpack Navigation?

Source: developer.android.comEasyHow cards are made

How do you pass data between Fragments with Jetpack Navigation?
Summary

Type-safe argument flow in Navigation.

Key points

Define args in nav graph XML, navigate with generated Directions, read with navArgs() in target Fragment.

Watch out for

Direct Fragment constructors, manual Bundles, or Activity Intent extras.

What's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

When sending an integer userId from a list Fragment to a detail Fragment using Jetpack Navigation, which approach follows modern type-safe practices?

  • a.Instantiate the detail Fragment with a constructor that accepts userId and add it via a FragmentTransaction
  • b.Declare the argument in the navigation graph XML, navigate with the generated Directions class, and retrieve it using the navArgs() delegateCorrect
  • c.Add the userId to the Activity Intent extras and read it in the detail Fragment's onCreate
  • d.Create a Bundle with a string key, set it as arguments on a new detail Fragment, and commit a FragmentTransaction
Why?

Safe Args generates type-safe Directions classes from nav graph XML and the navArgs() delegate reads them in the destination, preventing runtime key mismatches. A custom Fragment constructor is unsafe because the framework requires a no-argument constructor to recreate Fragments after process death.

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.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles