The Detail Screen
Personal Catalogue: Android (Kotlin & Compose)
Course 1 ยท Chapter 5 ยท Navigation, Detail and Delete
The list works, but tapping a row does nothing. This chapter adds a second screen that shows one item in full, moves between the two with Navigation Compose, and lets you delete an item after a confirmation. You will also see how a screen is handed the id of what to display, rather than the whole item, which is the habit that keeps screens independent of each other.
AlertDialog, which is therefore written from memory. Also, the search results mention Navigation 3, a newer, separate navigation library. This course uses the
established Navigation Compose (Navigation 2.x) library, as the outline promises, so if your tutorial or project template uses Navigation 3, the idea is the same but the code is not.
Navigation in One Picture
| Idea | Navigation Compose | Web catalogue equivalent |
|---|---|---|
| A screen's address | A route: a Kotlin class, e.g. DetailRoute(itemId = 7) | A URL, /items/7/ |
| The route table | A NavHost with one composable<Route> per screen | urls.py or the router |
| Going to a page | navController.navigate(DetailRoute(7)) | Following a link |
| The Back button | The back stack: navController.popBackStack() | The browser's back button |
| Reading the id | toRoute<DetailRoute>() | request.GET or the URL parameter |
Older tutorials write routes as strings such as "detail/{itemId}". Google's current guide recommends type-safe routes instead: each route is a @Serializable object or
class, so a mistyped argument is a compile error rather than a crash at runtime.
Dependencies
Type-safe routes use Kotlin serialization. In the version catalog add the navigation-compose library, the kotlinx-serialization-json library and the
org.jetbrains.kotlin.plugin.serialization plugin; apply the plugin and add both libraries in app/build.gradle.kts, exactly as you did for KSP and Hilt in Chapter 3. As
before, use the current versions from the release pages; the serialization plugin version normally equals your Kotlin version. Sync Gradle afterwards.
The Routes
Put both routes in ui/navigation/Routes.kt. The list needs no data, so it is an object; the detail screen needs one argument, so it is a data class.
The NavHost
The NavHost is the route table. It owns a NavController (created with rememberNavController()), starts at the list, and maps each route to a screen. Callbacks
such as onItemClick are where navigation actually happens: screens never see the controller, only plain lambdas, which keeps them previewable and testable.
Update MainActivity's setContent to call CatalogueNavHost() inside CatalogueTheme, and delete the now-unused CatalogueApp from Chapter 4.
Note that DetailScreen is called with no id: the view model reads it for itself, as the next section shows.
The Detail ViewModel
A NavBackStackEntry carries the route object, and Hilt gives the view model a SavedStateHandle holding the same arguments. Per Google's documentation, the route can be rebuilt from
that handle with toRoute<DetailRoute>(), so the id arrives type-checked. The view model exposes one of three states, which stops the screen having to guess between "still loading" and "that
item does not exist".
Two details. Deleting the item makes observeItem emit null at once, so the screen may briefly flip to NotFound before onDeleted() pops it off the back
stack; that flash is harmless, and NotFound also covers the real case of a stale id. And thanks to the CASCADE rule in Chapter 2, deleting an item removes its tag links but
leaves the tags themselves, with no extra code here.
The Detail Screen
Same pattern as Chapter 4: a stateful DetailScreen that collects the state, and a stateless DetailContent that draws it. The confirmation dialog is UI-only state: nothing
but this screen cares whether it is open, so it lives in the composable with rememberSaveable (which, as Chapter 4's third exercise showed, survives rotation).
The dialog text includes the item's title, so the user sees what they are about to delete. The confirm button is a plain TextButton; a destructive action should never happen on a single
accidental tap. Field returns early when a value is blank, so an item with no year or notes simply has no empty rows; in Chapter 6 these labels will change with the item type ("Author" for a
book, "Artist" for a CD, "Director" for a film).
Try It
Run the app, add a sample item with "+", tap its row (the detail screen appears), press the system Back button or the arrow (back to the list), open it again and tap the bin icon, then Cancel, then the bin icon
again and Delete. The row should vanish from the list with no refresh code, because the list is collecting a Flow.
NavigationStack with NavigationLink replaces the NavHost, the value you link with plays the part of
the route, and .confirmationDialog or .alert replaces AlertDialog. Both platforms pass an identifier or a lightweight value, not a copy of the whole record.
Challenges
Add the dependencies, Routes.kt, CatalogueNavHost, the detail view model and screen, and update MainActivity. Run the app and perform the full
add, open, back, delete-with-cancel, delete sequence. Write down which step you would expect to fail first if you forgot the serialization plugin.
Rotate the emulator while the delete dialog is open. Does the dialog survive? Explain why, and say what would change if showConfirm used plain remember.
The list passes only an Long id to the detail screen. Give two reasons that is better than passing the whole ItemWithDetails, and describe one bug you would
get if the detail screen kept its own copy of the item after the user edited it elsewhere.
Chapter 5 Quick Reference
- Routes:
@Serializable object ListRoute,@Serializable data class DetailRoute(val itemId: Long)(needs the serialization plugin and library) - Route table:
NavHost(navController, startDestination = ListRoute) { composable<ListRoute> { ... } } - Go:
navController.navigate(DetailRoute(id)); back:navController.popBackStack() - Read the argument in a Hilt view model:
savedStateHandle.toRoute<DetailRoute>().itemId - Screens receive lambdas, never the
NavController - Three detail states: Loading, NotFound, Found
- Dialog visibility is UI-only state:
rememberSaveable { mutableStateOf(false) };AlertDialog(onDismissRequest, title, text, confirmButton, dismissButton) - Delete cascades to the tag links (Chapter 2) but never deletes tags
@Serializable routes with composable<Route>; that toRoute() converts a back-stack entry
and SavedStateHandle.toRoute<T>() reads the route in a view model; that hiltViewModel() works with Compose navigation; and that a separate Navigation 3 library exists.Not checked, from general knowledge:
AlertDialog and its parameters (the search found nothing on it); that navigate(DetailRoute(id)) and
startDestination = ListRoute take route objects in your version; that a view model built by hiltViewModel() inside a composable<DetailRoute> receives the route arguments in its
SavedStateHandle (the pattern is standard, but I did not confirm it for Hilt specifically); the icons ArrowBack (the automirrored package), Delete and
Edit being in the core icon set; the @OptIn requirement; and all code, none of which was compiled.