tezvyn:

How do you pass data between Fragments with Jetpack Navigation?

Curated by the Tezvyn teamSource: developer.android.combeginner
How do you pass data between Fragments with Jetpack Navigation?

Tests knowledge of modern, type-safe argument passing. A good answer defines the argument in the nav graph XML, uses the Safe Args plugin to generate Directions/Args classes, navigates with the action, and retrieves the data in the destination via `by…

WHAT THIS TESTS: This question tests your familiarity with the modern, recommended patterns for Android UI development. It specifically probes whether you know how to use the Jetpack Navigation component's type-safe argument passing mechanism (Safe Args) versus older, more error-prone methods like manually creating Bundles. It's a check for up-to-date, best-practice knowledge.

A GOOD ANSWER COVERS: A strong answer walks through four distinct steps. First, define the argument in the navigation graph XML file within the destination fragment's tag, specifying its name and app:argType (e.g., string, integer). Second, enable and apply the Safe Args Gradle plugin in your build.gradle files, which auto-generates type-safe classes. Third, in the source fragment, use the generated NavDirections class to create an action and pass the data as a typed parameter, then navigate using that action object. For example, SourceFragmentDirections.actionToDestination(userId = "abc-123"). Fourth, in the destination fragment, retrieve the arguments using the generated Args class, typically with the by navArgs() Kotlin property delegate.

COMMON WRONG ANSWERS: A major red flag is suggesting manual Bundle creation (e.g., val bundle = Bundle().apply { putString("USER_ID", "123") } and passing it to navigate()). This is not type-safe and is the pre-Safe Args way of doing things. It indicates a lack of familiarity with modern tooling. Another less-than-ideal answer is immediately jumping to a shared ViewModel scoped to the Activity. While a valid pattern for sharing complex state, it's overkill for passing a simple, immutable ID from one screen to the next and isn't the direct answer to the question. Using Fragment constructors to pass data is a fundamental anti-pattern and a serious red flag.

LIKELY FOLLOW-UPS: Expect questions like, "When would you choose a shared ViewModel over navigation arguments?" (Answer: For complex, mutable data or state that needs to survive beyond a single navigation event). Or, "How do you pass a custom Parcelable object?" (Answer: You specify the fully qualified class name in the app:argType attribute). A follow-up on the benefits of Safe Args is also common (Answer: Compile-time safety, null safety, and avoiding key/type mismatch errors at runtime).

ONE CONCRETE EXAMPLE: In nav_graph.xml, the destination has <argument android:name="userId" app:argType="string" />. The source fragment calls val action = FirstFragmentDirections.actionFirstToSecond(userId = "user-99"); findNavController().navigate(action). The destination fragment retrieves it with private val args: SecondFragmentArgs by navArgs() and then accesses the value with args.userId.

Read the original → developer.android.com

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.

How do you pass data between Fragments with Jetpack Navigation? · Tezvyn