Bird
Raised Fist0
Android Kotlinmobile~20 mins

Navigation component setup in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Navigation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
Navigation Graph XML Structure
Which option correctly defines a navigation graph XML with two fragments named HomeFragment and DetailFragment?
A<navigation xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:app=\"http://schemas.android.com/apk/res-auto\">\n <fragment android:id=\"@+id/homeFragment\" android:name=\"com.example.HomeFragment\" />\n <fragment android:id=\"@+id/detailFragment\" android:name=\"com.example.DetailFragment\" />\n</navigation>
B<navigation xmlns:android=\"http://schemas.android.com/apk/res/android\">\n <fragment android:id=\"@+id/homeFragment\" android:name=\"com.example.HomeFragment\" />\n <fragment android:id=\"@+id/detailFragment\" android:name=\"com.example.DetailFragment\" />\n</navigation>
C<navgraph xmlns:android=\"http://schemas.android.com/apk/res/android\" app:startDestination=\"@id/homeFragment\">\n <fragment android:id=\"@+id/homeFragment\" android:name=\"com.example.HomeFragment\" />\n <fragment android:id=\"@+id/detailFragment\" android:name=\"com.example.DetailFragment\" />\n</navgraph>
D<navigation xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:app=\"http://schemas.android.com/apk/res-auto\" app:startDestination=\"@id/homeFragment\">\n <fragment android:id=\"@+id/homeFragment\" android:name=\"com.example.HomeFragment\" />\n <fragment android:id=\"@+id/detailFragment\" android:name=\"com.example.DetailFragment\" />\n</navigation>
Attempts:
2 left
💡 Hint
Remember to include both xmlns attributes and set the startDestination with the correct fragment id.
📝 Syntax
intermediate
2:00remaining
Correct Kotlin Code to Navigate Between Fragments
Which Kotlin code snippet correctly navigates from HomeFragment to DetailFragment using Navigation component?
Android Kotlin
class HomeFragment : Fragment(R.layout.fragment_home) {
    fun goToDetail() {
        // navigation code here
    }
}
AfindNavController().navigate(R.id.action_homeFragment_to_detailFragment)
BNavigation.findNavController(this).navigate(R.id.action_homeFragment_to_detailFragment)
CfindNavController().navigate(R.id.detailFragment)
DnavController.navigate(R.id.action_homeFragment_to_detailFragment)
Attempts:
2 left
💡 Hint
Use the fragment's extension function to get NavController and navigate using the action id.
❓ lifecycle
advanced
2:00remaining
Navigation Component and Fragment Lifecycle
What happens to the previous fragment's view when navigating to a new fragment using Navigation component with default settings?
AThe previous fragment's view is destroyed immediately after navigation.
BThe previous fragment's view remains in memory and visible behind the new fragment.
CThe previous fragment's view is replaced but the fragment instance stays alive.
DThe previous fragment's view is detached but kept in memory for quick return.
Attempts:
2 left
💡 Hint
Think about how Navigation component manages fragment views by default to save memory.
🔧 Debug
advanced
2:00remaining
Fixing Navigation Crash on Button Click
Given this Kotlin code inside a fragment, the app crashes when the button is clicked. What is the cause?
Android Kotlin
button.setOnClickListener {
    Navigation.findNavController(requireActivity()).navigate(R.id.action_homeFragment_to_detailFragment)
}
AThe action id is incorrect and does not exist in navigation graph.
BUsing requireActivity() instead of the button view causes findNavController to fail.
CThe button is not initialized before setting the listener.
DNavigation component is not added as a dependency.
Attempts:
2 left
💡 Hint
Check the parameter passed to findNavController to ensure it is a View inside a NavHostFragment.
🧠 Conceptual
expert
2:00remaining
Deep Link Handling in Navigation Component
Which statement about deep link handling in Navigation component is true?
ANavigation component does not support deep links; external libraries are needed.
BDeep links require manual intent handling in each fragment to navigate correctly.
CDeep links can be declared in the navigation graph XML and automatically navigate to the specified destination when triggered.
DDeep links only work if the app is already running in the foreground.
Attempts:
2 left
💡 Hint
Think about how Navigation component integrates with Android's intent system.

Practice

(1/5)
1. What is the main purpose of the Navigation component in Android development?
easy
A. To manage screen transitions and navigation paths easily
B. To handle database operations
C. To style UI components
D. To manage network requests

Solution

  1. Step 1: Understand Navigation component role

    The Navigation component is designed to simplify moving between screens in an app.
  2. Step 2: Compare with other options

    Database, styling, and network are unrelated to navigation tasks.
  3. Final Answer:

    To manage screen transitions and navigation paths easily -> Option A
  4. Quick Check:

    Navigation component = screen transitions [OK]
Hint: Navigation component = screen changes manager [OK]
Common Mistakes:
  • Confusing navigation with database handling
  • Thinking it styles UI elements
  • Assuming it manages network calls
2. Which XML element is used to define the navigation graph in Android?
easy
A. <navGraph>
B. <navigation>
C. <navHost>
D. <navController>

Solution

  1. Step 1: Identify the navigation graph element

    The navigation graph XML uses the <navigation> root element to define screens and paths.
  2. Step 2: Check other elements

    <navHost> and <navController> are not XML tags; <navGraph> is incorrect.
  3. Final Answer:

    <navigation> -> Option B
  4. Quick Check:

    Navigation graph XML uses <navigation> [OK]
Hint: Navigation graph XML root is <navigation> tag [OK]
Common Mistakes:
  • Using <navGraph> which is invalid
  • Confusing NavHostFragment with XML tag
  • Mistaking NavController for XML element
3. Given this code snippet, what screen will be displayed first?
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.HomeFragment" />

    <fragment
        android:id="@+id/detailFragment"
        android:name="com.example.DetailFragment" />

</navigation>
medium
A. No screen shown
B. DetailFragment
C. HomeFragment
D. Error due to missing NavHostFragment

Solution

  1. Step 1: Identify startDestination attribute

    The navigation graph sets startDestination="@id/homeFragment", so HomeFragment is the first screen.
  2. Step 2: Confirm fragment IDs

    HomeFragment and DetailFragment are defined; startDestination points to HomeFragment.
  3. Final Answer:

    HomeFragment -> Option C
  4. Quick Check:

    startDestination = HomeFragment [OK]
Hint: Look for startDestination attribute in navigation XML [OK]
Common Mistakes:
  • Choosing DetailFragment by mistake
  • Assuming no screen shows without NavHostFragment
  • Confusing fragment IDs
4. You wrote this code to navigate but get a crash:
findNavController().navigate(R.id.action_home_to_detail)
What is the most likely cause?
medium
A. All of the above
B. Incorrect action ID in navigation graph
C. Using findNavController() outside a Fragment or Activity
D. Missing NavHostFragment in layout

Solution

  1. Step 1: Check NavHostFragment presence

    Without NavHostFragment in layout, navigation cannot work and causes crash.
  2. Step 2: Verify action ID and context

    If action ID is wrong or findNavController() is called outside proper context, app crashes.
  3. Final Answer:

    All of the above -> Option A
  4. Quick Check:

    Missing NavHost, wrong ID, or wrong context cause crash [OK]
Hint: Check NavHost, action ID, and context if navigation crashes [OK]
Common Mistakes:
  • Ignoring NavHostFragment setup
  • Using wrong action ID
  • Calling findNavController() in wrong place
5. You want to navigate from HomeFragment to DetailFragment using the Navigation component. Which steps are required to set this up correctly?
hard
A. Only add NavHostFragment in layout and call navigate() in code
B. Add action in nav_graph.xml and call navigate() without defining fragments
C. Define fragments in nav_graph.xml and call navigate() without NavHostFragment
D. Define fragments and action in nav_graph.xml, add NavHostFragment in layout, call findNavController().navigate(actionId)

Solution

  1. Step 1: Define fragments and action in navigation graph

    Both fragments and the action connecting them must be declared in nav_graph.xml.
  2. Step 2: Add NavHostFragment in activity layout

    NavHostFragment hosts the navigation and displays fragments.
  3. Step 3: Use findNavController().navigate(actionId) in code

    This triggers navigation along the defined action.
  4. Final Answer:

    Define fragments and action in nav_graph.xml, add NavHostFragment in layout, call findNavController().navigate(actionId) -> Option D
  5. Quick Check:

    Fragments + action + NavHost + navigate() = correct setup [OK]
Hint: All three: graph, NavHost, and navigate() needed [OK]
Common Mistakes:
  • Skipping NavHostFragment in layout
  • Not defining action in nav_graph.xml
  • Calling navigate() without proper setup
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard