Code

Create Screen.kt — screens as NavKeys

First, the screen "addresses". Create the navigation package and Screen.kt inside it.

  • sealed class Screen : NavKey — a finite list of screens. sealed means "all cases are right here", so the compiler helps you not forget any.
  • data object Menu, data object Cart — screens with no data.
  • data class MenuItemDetail(val itemId: Int) — a screen with an argument (which pizza to show). The argument travels with the "address".
  • @Serializable — required: this is how Nav3 saves and restores the stack if the system kills the process (e.g. the phone running low on memory).

For now we list only the screens that already exist (Menu, MenuItemDetail, Cart). Login, Scan, OrderHistory are added in their own lessons — you can't route to a screen that doesn't exist yet.

Verify. The project compiles with no errors. Screen isn't used anywhere yet — we wire it in the next step. If NavKey or @Serializable are red, check that build.gradle.kts has the navigation3 libraries and the kotlin.serialization plugin (they come with the Compose template + Nav3).

Tip. Notice the difference between data object (one fixed screen, e.g. the cart) and a data class with an argument (MenuItemDetail(itemId)). Once the menu is real in lesson 5, tapping a pizza can navigate to MenuItemDetail(item.id) — and the detail screen knows which pizza to show.