Challenge - 5 Problems
Compose Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
intermediate
2:00remaining
What is the output when navigating between two composables?
Consider two composables: HomeScreen and DetailScreen. When a button in HomeScreen is clicked, the app navigates to DetailScreen. What will be displayed after clicking the button?
Android Kotlin
import androidx.compose.material.* import androidx.compose.runtime.* import androidx.navigation.compose.* @Composable fun AppNavHost() { val navController = rememberNavController() NavHost(navController, startDestination = "home") { composable("home") { Button(onClick = { navController.navigate("detail") }) { Text("Go to Detail") } } composable("detail") { Text("Detail Screen") } } }
Attempts:
2 left
💡 Hint
Think about what composable is shown at the start and what happens on button click.
✗ Incorrect
Initially, the HomeScreen composable shows a button labeled 'Go to Detail'. When clicked, navController navigates to 'detail' route, showing the DetailScreen composable with text 'Detail Screen'.
❓ ui_behavior
intermediate2:00remaining
What happens if you navigate to the same composable multiple times?
In a Jetpack Compose app, if you navigate repeatedly to the same composable route without popping the back stack, what is the expected behavior?
Android Kotlin
val navController = rememberNavController()
Button(onClick = { navController.navigate("profile") }) {
Text("Go to Profile")
}
// Assume 'profile' composable is defined in NavHostAttempts:
2 left
💡 Hint
Think about how navigation back stack works in Jetpack Compose.
✗ Incorrect
Each call to navigate adds a new entry to the back stack, so multiple instances of the same composable route can exist.
❓ lifecycle
advanced2:00remaining
Which lifecycle event occurs when navigating away from a composable?
When navigating from Composable A to Composable B using Jetpack Compose Navigation, which lifecycle event is triggered for Composable A?
Attempts:
2 left
💡 Hint
Think about how Compose handles cleanup when a composable leaves the screen.
✗ Incorrect
When a composable leaves the composition due to navigation, DisposableEffect's onDispose callback is triggered to clean up resources.
📝 Syntax
advanced2:00remaining
Which code snippet correctly passes an argument during navigation?
You want to navigate to a composable named 'detail' and pass a userId as an argument. Which code snippet correctly performs this navigation?
Attempts:
2 left
💡 Hint
Think about how Jetpack Compose Navigation encodes arguments in routes.
✗ Incorrect
In Jetpack Compose Navigation, arguments are passed as part of the route string using placeholders like 'detail/{userId}'. So navigating with 'detail/$userId' is correct.
🔧 Debug
expert3:00remaining
Why does this navigation code cause a crash?
Given the code below, why does the app crash when navigating to 'profile'?
Android Kotlin
NavHost(navController, startDestination = "home") { composable("home") { HomeScreen(navController) } composable("profile/{userId}") { backStackEntry -> val userId = backStackEntry.arguments?.getString("userId")!! ProfileScreen(userId) } } // Navigation call: navController.navigate("profile")
Attempts:
2 left
💡 Hint
Check if all required arguments are provided when navigating.
✗ Incorrect
The 'profile/{userId}' route requires a userId argument. Navigating with 'profile' without userId causes a crash due to missing argument.