Nested navigation graphs help organize app screens into smaller groups. This makes your app easier to manage and navigate.
Nested navigation graphs in Android Kotlin
val navController = findNavController(R.id.nav_host_fragment) val nestedGraph = navController.navInflater.inflate(R.navigation.nested_graph) navController.graph.addDestination(nestedGraph)
You define nested graphs in separate XML files inside the res/navigation folder.
Use <include> tag in your main navigation XML to include nested graphs.
settings_graph.<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_graph" app:startDestination="home_fragment"> <fragment android:id="@+id/home_fragment" android:name="com.example.HomeFragment" android:label="Home" /> <include app:graph="@navigation/settings_graph" /> </navigation>
<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/settings_graph" app:startDestination="settings_fragment"> <fragment android:id="@+id/settings_fragment" android:name="com.example.SettingsFragment" android:label="Settings" /> <fragment android:id="@+id/profile_fragment" android:name="com.example.ProfileFragment" android:label="Profile" /> </navigation>
This is a simple activity that hosts the navigation component. The navigation XML files define the nested graphs.
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.navigation.findNavController import androidx.navigation.fragment.NavHostFragment class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment val navController = navHostFragment.navController // The main graph includes the nested settings graph via XML // Navigation works seamlessly between home and settings/profile screens } }
Nested graphs help keep navigation organized and modular.
Time complexity: Navigation actions are fast and constant time.
Common mistake: Forgetting to set the start destination in nested graphs causes navigation errors.
Use nested graphs when you want to separate flows logically, instead of putting all screens in one big graph.
Nested navigation graphs group related screens for better organization.
Use <include> in XML to add nested graphs inside main graph.
This makes your app easier to maintain and navigate.
Practice
What is the main purpose of using nested navigation graphs in Android apps?
Solution
Step 1: Understand navigation graph purpose
Navigation graphs define how screens connect and transition in an app.Step 2: Role of nested graphs
Nested graphs group related screens to keep navigation organized and maintainable.Final Answer:
To group related screens for better organization and easier navigation -> Option AQuick Check:
Nested graphs = group related screens [OK]
- Thinking nested graphs duplicate code
- Believing nested graphs disable navigation
- Confusing nested graphs with replacing fragments
Which XML tag is used to include a nested navigation graph inside a main navigation graph?
Solution
Step 1: Identify tag for including nested graphs
The <include> tag is used to insert one XML layout or graph inside another.Step 2: Confirm usage in navigation graphs
To add a nested navigation graph inside the main graph, <include> is the correct tag.Final Answer:
<include> -> Option DQuick Check:
Include tag adds nested graph [OK]
- Using <graph> instead of <include>
- Confusing <fragment> with <include>
- Mistaking <navigation> as include tag
Given this main navigation graph XML snippet:
<navigation xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:id=\"@+id/main_graph\"
app:startDestination=\"@id/homeFragment\">
<include app:graph=\"@navigation/profile_graph\" />
<fragment android:id=\"@+id/homeFragment\" android:name=\"com.example.HomeFragment\" />
</navigation>
What happens when the app starts?
Solution
Step 1: Check startDestination in main graph
The main graph's startDestination is set to homeFragment explicitly.Step 2: Effect of including profile_graph
Including profile_graph adds its screens but does not change main graph's startDestination.Final Answer:
The app starts at HomeFragment as the start destination -> Option AQuick Check:
Start destination in main graph controls start [OK]
- Assuming nested graph startDestination overrides main
- Thinking app crashes without startDestination in nested graph
- Believing included graphs start automatically
What is wrong with this nested navigation graph XML snippet?
<navigation xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:id=\"@+id/profile_graph\">
<fragment android:id=\"@+id/profileFragment\" android:name=\"com.example.ProfileFragment\" />
</navigation>
When included in main graph, navigation to profileFragment fails. Why?
Solution
Step 1: Check required attributes in navigation graph
Every navigation graph must have a startDestination attribute to define entry point.Step 2: Identify missing startDestination
The profile_graph lacks startDestination, so navigation fails when included.Final Answer:
Missing <code>startDestination</code> attribute in profile_graph -> Option BQuick Check:
startDestination required in nested graphs [OK]
- Thinking plus sign in ID causes error
- Assuming fragment name format is wrong
- Ignoring missing startDestination attribute
You want to create a nested navigation graph for a settings section with two fragments: SettingsListFragment and SettingsDetailFragment. Which is the correct way to define the nested graph XML?
<navigation xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:id=\"@+id/settings_graph\"
app:startDestination=\"@id/settingsListFragment\">
<fragment android:id=\"@+id/settingsListFragment\" android:name=\"com.example.SettingsListFragment\" />
<fragment android:id=\"@+id/settingsDetailFragment\" android:name=\"com.example.SettingsDetailFragment\" />
</navigation>
How should you include this nested graph inside the main navigation graph?
Solution
Step 1: Define nested graph with startDestination and fragments
The settings_graph XML correctly defines startDestination and fragments.Step 2: Include nested graph in main graph
Use <include app:graph=\"@navigation/settings_graph\" /> to add nested graph inside main graph.Final Answer:
Use <include app:graph=\"@navigation/settings_graph\" /> inside main graph -> Option CQuick Check:
Include tag adds nested graph correctly [OK]
- Using fragment tag instead of include
- Copying fragments instead of nesting
- Using navigation tag incorrectly inside main graph
Ready to prove it?
Pass the test and this pattern moves unknown
