Navigation (Fragment & Compose) Interview Questions
UI › Material & Nav
How does the Jetpack Navigation component work?
What a strong answer covers: You define a navigation graph of destinations and a NavHost that shows the current one; a NavController moves between them, managing the back stack, arguments, transitions and deep links. It works with Fragments (an XML graph or Kotlin DSL) and Compose (NavHost with composable destinations). Back handling and the Up button integrate with it.
How do you pass arguments between screens, and what shouldn't you pass?
What a strong answer covers: With type-safe navigation in Navigation 2.8+, routes are @Serializable classes or objects, and you read them with toRoute() or from SavedStateHandle in the ViewModel. Older apps use string routes or Safe Args. Pass small things like an id, then load the object from a repository in the destination. Don't pass large or complex objects: arguments go into the saved state Bundle.
How do you return a result to the previous screen?
What a strong answer covers: In Navigation, set a value on the previous back stack entry's SavedStateHandle (navController.previousBackStackEntry?.savedStateHandle?.set(key, value)) and read it as a flow in the previous screen. With Fragments, the Fragment Result API also works. A shared ViewModel scoped to a navigation graph is another option for flows of several screens.
How do you scope a ViewModel to a nested navigation graph, and why would you?
What a strong answer covers: Group a flow like checkout or onboarding into a nested graph and get its back stack entry, then call hiltViewModel(parentEntry) in Compose or by navGraphViewModels(R.id.graph) with Fragments. The ViewModel is shared by every screen in the flow and cleared when the whole flow is popped, so state lives exactly as long as the flow.
How would you handle navigation in a multi-module app so feature modules don't depend on each other?
What a strong answer covers: Each feature exposes its route type and a NavGraphBuilder extension to register its screens, in an api module; the app module builds the NavHost from all features. Features navigate to each other through route types in lightweight api modules, or through callbacks passed in by the app module, so feature-a never depends on feature-b's implementation. Deep links can also act as the contract. Navigation 3 is also emerging, where you own the back stack as a list.