Skip to content
tezvyn:

How to use a nested navigation graph for a login flow?

Source: developer.android.comMediumHow cards are made

How to use a nested navigation graph for a login flow?

Tests your ability to structure complex UI flows using Navigation Component. A great answer defines a separate login graph XML, includes it in the main graph, and navigates to the graph's ID. This encapsulates the flow, making it reusable.

What's really being asked

This question tests your understanding of architectural patterns within the Android Navigation Component. The interviewer wants to see if you can go beyond simple screen-to-screen navigation and use tools like nested graphs to create modular, encapsulated, and reusable UI flows. It's a test of clean code and separation of concerns, not just API knowledge.

The full answer

A strong answer walks through three distinct steps and then explains the benefits. First, create a new navigation graph file, for example login_nav_graph.xml. This file defines the three destinations (Welcome, Email, Password), the actions between them, and sets its own start destination (app:startDestination="@id/welcome_screen"). Second, in your main navigation graph (main_nav_graph.xml), you embed this flow using an <include> tag: <include app:graph="@navigation/login_nav_graph" />. This makes the entire login flow a single destination in the main graph. Third, to start the flow, you navigate to the ID of the nested graph itself, not one of its screens: navController.navigate(R.id.login_nav_graph). The primary benefits are encapsulation (the main graph doesn't know or care about the internal screens of the login flow) and reusability (the same login flow can be triggered from multiple places in the app with one line of code).

The mistakes people make

The most common mistake is describing the correct setup but then navigating incorrectly. A candidate who says navController.navigate(R.id.welcome_screen) from outside the login graph has missed the point. This creates a tight coupling and breaks encapsulation, as the calling code now needs to know the internal implementation detail of the login flow's starting screen. If the login flow changes to start on a phone number screen instead, all call sites break. A less severe red flag is just describing the screens and actions but failing to mention creating a separate, included XML file, which suggests a lack of practical experience.

What usually comes next

Expect questions like: "How would you return a result, like 'login successful,' from the nested graph back to the previous screen?" (Answer: Use the SavedStateHandle of the previousBackStackEntry). Another is, "How does this concept relate to feature modules?" (Answer: A nested graph is the ideal way to define the navigation for a self-contained feature module, keeping its UI flow completely isolated from the main app module).

A concrete example

Imagine your main graph has 20 destinations. A login flow adds 3 more. Without nesting, your main_nav_graph.xml now has 23 destinations, increasing its complexity. With nesting, the main graph has only 21 destinations (the original 20 plus a single login_nav_graph destination). The complexity of the login flow is neatly contained in its own file. This simplifies the main graph by nearly 10% and prevents another developer from accidentally creating an action that navigates from a settings screen directly into the middle of your login flow (e.g., to the password screen), which would likely crash the app.

Interview question

To start an encapsulated login flow defined in a nested graph, what destination ID should your NavController navigate to?

  • a.The ID of the nested navigation graph resource itself.Correct
  • b.The ID of a global action that points to the nested graph's start destination.
  • c.The ID of the NavHostFragment that hosts the navigation graphs.
  • d.The ID of the start destination screen within the nested graph.
Why?

Navigating to the graph's ID treats the entire flow as a single, encapsulated destination. Navigating directly to a screen inside the graph breaks this encapsulation by requiring the caller to know the graph's internal structure.

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