Navigation (Fragment & Compose) Flashcards
UI › Material & Nav
- What are the three core parts of the Navigation component?
- NavController (drives navigation and owns the back stack), NavGraph (the set of destinations and how they connect), and a NavHost (NavHostFragment for Views, NavHost composable for Compose) that swaps the current destination in and out.
- What is Safe Args and what problem does it solve?
- A Gradle plugin for XML/Fragment graphs that generates type-safe Directions and Args classes from the nav graph, so arguments are passed and read with compile-time type checking instead of error-prone manual Bundle keys.
- How do type-safe routes work in Navigation Compose?
- You define routes as @Serializable Kotlin classes/objects (kotlinx.serialization), register them with composable<Route> { }, navigate by passing an instance, and read arguments via backStackEntry.toRoute<Route>(). Requires the kotlin serialization plugin.
- Explain explicit vs implicit deep links.
- Explicit deep links build a PendingIntent/NavDeepLinkBuilder (or call navigate with a URI) to jump directly to a destination, typically from notifications. Implicit deep links match an external URI via intent-filter / navDeepLink so the OS or browser opens the app at that destination.
- What do popUpTo and launchSingleTop do?
- popUpTo(route) pops the back stack up to that destination before navigating (inclusive=true also removes it); launchSingleTop reuses the existing top instance instead of pushing a duplicate. Together they prevent stacking duplicate screens, e.g. on bottom-nav tabs.
- How do you share a ViewModel between several destinations?
- Scope the ViewModel to a navigation graph (navGraphViewModels(R.id.graph) in Fragments, or hiltViewModel/viewModel keyed to the parent back stack entry in Compose). It lives as long as that graph is on the back stack, letting destinations share state.
- What is single-activity architecture and why is it recommended?
- One Activity hosts a NavHost and all screens are destinations (Fragments or composables). It centralizes navigation and the back stack, simplifies lifecycle and shared state, and removes manual Activity/Intent and FragmentTransaction boilerplate.
- How do saveState and restoreState help with bottom navigation?
- When switching tabs, popUpTo(saveState = true) saves the outgoing tab's back stack, and navigating with restoreState = true (plus launchSingleTop) restores the destination tab's previously saved stack, so each tab keeps independent navigation history.
- What is a nested navigation graph and when do you use one?
- A subgraph with its own start destination embedded in a parent graph. It modularizes a feature/flow (e.g. onboarding, checkout), provides a clean ViewModel scope, and lets you navigate to the subgraph as a single destination while keeping internal routes encapsulated.