0
0
Android Kotlinmobile~10 mins

NavHost and NavController in Android Kotlin - UI Render Trace

Choose your learning style9 modes available
Component - NavHost and NavController

This UI component manages navigation between different screens in an Android app. NavHost acts like a container that shows the current screen, while NavController controls which screen is shown and handles moving between screens.

Widget Tree
NavHostFragment
└── NavController
    ├── DestinationScreen1
    └── DestinationScreen2
The NavHostFragment is the main container in the layout. It holds the NavController, which manages navigation destinations (screens). The NavController switches between DestinationScreen1 and DestinationScreen2 based on user actions.
Render Trace - 3 Steps
Step 1: NavHostFragment
Step 2: NavController
Step 3: Navigation Action
State Change - Re-render
Trigger:User clicks a button to navigate from DestinationScreen1 to DestinationScreen2
Before
NavController shows DestinationScreen1 inside NavHost
After
NavController shows DestinationScreen2 inside NavHost
Re-renders:NavHostFragment content updates to DestinationScreen2 UI
UI Quiz - 3 Questions
Test your understanding
What is the role of NavHost in Android navigation?
AIt stores user data for the app.
BIt controls the navigation logic between screens.
CIt acts as a container that displays the current screen.
DIt handles network requests.
Key Insight
Using NavHost and NavController together lets you manage screen changes smoothly in Android apps. NavHost shows the current screen, and NavController handles navigation commands. This separation keeps your UI organized and navigation logic clear.