0
0
Android Kotlinmobile~10 mins

NavHost and NavController in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a NavHost in a Compose UI.

Android Kotlin
NavHost(navController = [1], startDestination = "home") {
    composable("home") { HomeScreen() }
}
Drag options to blanks, or click blank then click option'
AnavController
BrememberNavController()
CNavController()
DNavHostController()
Attempts:
3 left
💡 Hint
Common Mistakes
Using NavController() directly causes errors because it needs to be remembered.
Using undefined variables like navController without initialization.
2fill in blank
medium

Complete the code to navigate to the "details" screen using NavController.

Android Kotlin
navController.[1]("details")
Drag options to blanks, or click blank then click option'
AgoTo
Bmove
Cnavigate
DswitchTo
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent functions like goTo or move causes compile errors.
Confusing navigation with fragment transactions.
3fill in blank
hard

Fix the error in the code to get the current back stack entry from NavController.

Android Kotlin
val currentBackStackEntry = navController.[1]
Drag options to blanks, or click blank then click option'
AcurrentBackStackEntryAsState
BcurrentBackStackEntryAsState()
CcurrentBackStackEntry
DgetCurrentBackStackEntry()
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses makes it a function call which does not exist.
Using the property without 'AsState' does not provide Compose state.
4fill in blank
hard

Fill both blanks to define a composable destination with a route and content.

Android Kotlin
NavHost(navController, startDestination = "home") {
    composable(route = [1]) { [2] }
}
Drag options to blanks, or click blank then click option'
A"home"
BHomeScreen()
C"details"
DDetailsScreen()
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing route strings and composable calls.
Forgetting quotes around route strings.
5fill in blank
hard

Fill all three blanks to create a NavHost with a remembered NavController and navigate to "profile" on button click.

Android Kotlin
val navController = [1]()

NavHost(navController = navController, startDestination = "home") {
    composable("home") {
        Button(onClick = { navController.[2]("profile") }) {
            Text(text = [3])
        }
    }
    composable("profile") { ProfileScreen() }
}
Drag options to blanks, or click blank then click option'
ArememberNavController
Bnavigate
C"Go to Profile"
DnavigateTo
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function names like navigateTo or missing parentheses.
Forgetting quotes around button text.