Navigation lets users move between different screens in your app. It helps organize your app into small parts called composables.
Navigating between composables in Android Kotlin
Introduction
Syntax
Android Kotlin
val navController = rememberNavController() NavHost(navController, startDestination = "screen1") { composable("screen1") { Screen1(navController) } composable("screen2") { Screen2(navController) } } // To navigate: navController.navigate("screen2")
NavController controls navigation actions.
NavHost holds the navigation graph with all screens.
Examples
Android Kotlin
navController.navigate("profile")Android Kotlin
navController.popBackStack()
Android Kotlin
composable("details/{itemId}") { backStackEntry -> val itemId = backStackEntry.arguments?.getString("itemId") DetailsScreen(itemId) }
Android Kotlin
navController.navigate("details/42")Sample App
This app has two screens: Home and Details. On Home, a button navigates to Details. On Details, a button goes back to Home.
Android Kotlin
import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.navigation.compose.* class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { val navController = rememberNavController() NavHost(navController, startDestination = "home") { composable("home") { HomeScreen(navController) } composable("details") { DetailsScreen(navController) } } } } } @Composable fun HomeScreen(navController: NavHostController) { Scaffold( topBar = { TopAppBar(title = { Text("Home") }) }, content = { Button(onClick = { navController.navigate("details") }) { Text("Go to Details") } } ) } @Composable fun DetailsScreen(navController: NavHostController) { Scaffold( topBar = { TopAppBar(title = { Text("Details") }) }, content = { Button(onClick = { navController.popBackStack() }) { Text("Back to Home") } } ) }
Important Notes
Always use rememberNavController() inside a composable to keep navigation state.
Use clear route names to avoid confusion.
Passing data between screens requires defining route parameters and reading them from backStackEntry.
Summary
Navigation connects different composables as screens.
Use NavHost and NavController to manage navigation.
Navigate with navController.navigate(route) and go back with popBackStack().
Practice
1. What is the main purpose of
NavController in Jetpack Compose navigation?easy
Solution
Step 1: Understand the role of NavController
NavController is responsible for controlling navigation actions between composables in an app.Step 2: Differentiate from other components
It does not create UI elements, handle databases, or style components; those are handled by other parts of Compose or Android.Final Answer:
To manage navigation between composable screens -> Option CQuick Check:
NavController manages navigation [OK]
Hint: NavController controls screen changes in Compose [OK]
Common Mistakes:
- Confusing NavController with UI creation
- Thinking NavController handles data or styling
- Mixing NavController with ViewModel
2. Which of the following is the correct syntax to navigate to a composable with route "profile" using NavController named
navController?easy
Solution
Step 1: Recall the navigation method
The correct method to navigate isnavigate()on NavController.Step 2: Check method names
Onlynavigate()is valid; others likegoTo(),move(), oropen()do not exist.Final Answer:
navController.navigate("profile") -> Option AQuick Check:
Use navigate() to change screens [OK]
Hint: Use navigate() method on NavController [OK]
Common Mistakes:
- Using non-existent methods like goTo or move
- Forgetting to pass route as a string
- Calling navigation on wrong object
3. Given this code snippet inside a composable:
val navController = rememberNavController()
Button(onClick = { navController.navigate("details") }) {
Text("Go to Details")
}
What happens when the button is clicked?medium
Solution
Step 1: Understand the button's onClick action
The button callsnavController.navigate("details")which triggers navigation to the "details" route.Step 2: Consider NavHost presence
Assuming NavHost is set up correctly with "details" route, navigation will succeed and show that screen.Final Answer:
The app navigates to the composable with route "details" -> Option AQuick Check:
Button triggers navigate() to details [OK]
Hint: Button click calls navigate() to change screen [OK]
Common Mistakes:
- Assuming navigate() does nothing
- Confusing navigation with app closing
- Ignoring NavHost setup requirement
4. Consider this code snippet:
navController.navigate("home")
navController.popBackStack()
What is the likely problem here?medium
Solution
Step 1: Analyze the sequence of calls
Callingnavigate("home")pushes a new screen, but immediately callingpopBackStack()removes the top screen.Step 2: Understand effect on navigation
This means the navigation to "home" is undone immediately, so user may not see the new screen.Final Answer:
Calling popBackStack() immediately after navigate() may prevent navigation -> Option BQuick Check:
popBackStack() right after navigate() cancels navigation [OK]
Hint: Don't popBackStack() right after navigate() [OK]
Common Mistakes:
- Thinking popBackStack() is invalid
- Assuming navigation always succeeds despite popBackStack()
- Ignoring call order effects
5. You want to navigate from a composable "list" to "details/{itemId}" passing an item ID as argument. Which code correctly navigates to details screen with itemId = 42?
hard
Solution
Step 1: Understand route parameter usage
When route has a placeholder like "details/{itemId}", you replace {itemId} with actual value in the route string.Step 2: Check each option
navController.navigate("details/42") correctly replaces {itemId} with 42. navController.navigate("details") misses the parameter. navController.navigate("details?itemId=42") uses query syntax which is not standard here. navController.navigate("details/{itemId}") uses the placeholder literally, which won't work.Final Answer:
navController.navigate("details/42") -> Option DQuick Check:
Replace {itemId} with actual value in route [OK]
Hint: Replace placeholders with actual values in route string [OK]
Common Mistakes:
- Passing route with placeholder instead of value
- Using query parameters incorrectly
- Omitting required route arguments
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
