Why navigation, and what Nav3 is
You have two screens — the menu and the cart — but they don't live together. You need navigation: how to move from one to the other and back.
Android has Navigation 3 (Nav3) for this — a new approach. Old Navigation 2 kept a separate "graph"; Nav3's idea is simpler: the back stack is just a list of screens that you control.
backStack = [ Menu ] ← on launch
tap the cart:
backStack = [ Menu, Cart ] ← Cart "on top"
press Back:
backStack = [ Menu ] ← Cart pops off, back to Menu
Three parts:
Screen— each destination as aNavKey(Menu,Cart,MenuItemDetail(itemId)). Not UI — an "address".rememberNavBackStack(Screen.Menu)— creates the stack starting at the menu; it survives rotation.NavDisplay+entryProvider— maps eachScreento its composable and draws the top of the stack.
Navigating = pushing onto the stack (backStack.add(Screen.Cart)). Going back = the system pops the top when you press Back.
Worth knowing (verify the docs). Navigation 3 is still alpha — the API and imports (
androidx.navigation3.runtime,androidx.navigation3.ui) shift between alpha releases. This course's code is written against1.1.4; if your version is newer, some names may differ — check the official docs. The idea (screens as keys, the stack as a list) stays the same.
Let's build: first the screen "addresses", then the graph.