When a nav graph is more than you need
Jetpack Navigation (and Navigation Compose for Compose Multiplatform) gives you routes, a back stack, deep links and screen transitions. Large apps need those. An app with three or four screens that follow a fixed flow uses few of them. The question you actually have to answer is: which screen am I on, and which screen does this event lead to?
You can write that question as a small state machine with a Kotlin sealed interface. This post uses a tic-tac-toe game with three screens: Welcome, Game and Result. I built and tested the code on Kotlin 2.4.10 with Compose Multiplatform 1.11.1.
Screens are states
Each screen is a state. The data a screen needs travels inside that state:
sealed interface Screen {
data object Welcome : Screen
data class Game(val vsCpu: Boolean) : Screen
data class Result(val winner: String?, val vsCpu: Boolean) : Screen
}
Game needs to know whether you play against the CPU or a friend. Result needs the winner (null means a draw) and the mode just played, so "Play again" keeps the same mode. There are no string routes and no arguments to parse.
Events are inputs
What the user does to change screens becomes events:
sealed interface NavEvent {
data class Start(val vsCpu: Boolean) : NavEvent
data class Finish(val winner: String?) : NavEvent
data object PlayAgain : NavEvent
data object Back : NavEvent
}
One function decides the next screen
All navigation rules live in one pure function:
fun nextScreen(current: Screen, event: NavEvent): Screen = when (current) {
Screen.Welcome -> when (event) {
is NavEvent.Start -> Screen.Game(vsCpu = event.vsCpu)
else -> current
}
is Screen.Game -> when (event) {
is NavEvent.Finish -> Screen.Result(winner = event.winner, vsCpu = current.vsCpu)
NavEvent.Back -> Screen.Welcome
else -> current
}
is Screen.Result -> when (event) {
NavEvent.PlayAgain -> Screen.Game(vsCpu = current.vsCpu)
NavEvent.Back -> Screen.Welcome
else -> current
}
}
Because Screen is sealed, when (current) must handle every screen or the compiler reports an error. Add a screen and forget its rules, and the build fails right away. An event that makes no sense on the current screen (such as Back on Welcome) just returns the same screen.
I wrote else -> current for the remaining events on purpose. It keeps the code short, but the compiler won't remind you when you add a new NavEvent. If you want that reminder too, list every event instead of using else.
Hold the state and draw the screen
A small class holds the current screen in a StateFlow:
class Navigator {
private val _screen = MutableStateFlow<Screen>(Screen.Welcome)
val screen: StateFlow<Screen> = _screen.asStateFlow()
fun send(event: NavEvent) {
_screen.update { current -> nextScreen(current, event) }
}
}
The root composable reads the state and picks a screen with when:
@Composable
fun AppHost(navigator: Navigator) {
val screen by navigator.screen.collectAsState()
when (val current = screen) {
Screen.Welcome -> Button(onClick = { navigator.send(NavEvent.Start(vsCpu = true)) }) {
Text("Play vs CPU")
}
is Screen.Game -> Button(onClick = { navigator.send(NavEvent.Finish(winner = "X")) }) {
Text(if (current.vsCpu) "Game vs CPU" else "Game vs friend")
}
is Screen.Result -> Button(onClick = { navigator.send(NavEvent.PlayAgain) }) {
Text("Winner: ${current.winner ?: "draw"}. Play again")
}
}
}
In a real app each branch calls its own screen composable. Here each screen is a single button to keep the code short.
Test the navigation flow without UI
Because nextScreen is a pure function, a test just calls it and compares the result:
class NavigationTest {
@Test
fun finishingAGameShowsTheResult() {
val next = nextScreen(Screen.Game(vsCpu = true), NavEvent.Finish(winner = "O"))
assertEquals(Screen.Result(winner = "O", vsCpu = true), next)
}
@Test
fun playAgainKeepsTheMode() {
val next = nextScreen(Screen.Result(winner = null, vsCpu = false), NavEvent.PlayAgain)
assertEquals(Screen.Game(vsCpu = false), next)
}
@Test
fun welcomeIgnoresBack() {
assertEquals(Screen.Welcome, nextScreen(Screen.Welcome, NavEvent.Back))
}
@Test
fun navigatorFollowsEvents() {
val navigator = Navigator()
navigator.send(NavEvent.Start(vsCpu = true))
navigator.send(NavEvent.Finish(winner = "X"))
assertEquals(Screen.Result(winner = "X", vsCpu = true), navigator.screen.value)
}
}
These four tests run in commonTest, with no emulator and no iOS device.
The system Back button
This setup is not yet connected to the OS Back button (or the back swipe gesture). You need a thin layer on each platform that calls navigator.send(NavEvent.Back). On Android that is BackHandler from androidx.activity:activity-compose. The rule for where Back goes from each screen stays in nextScreen, so the platform part only forwards the event.
When to go back to a navigation library
A hand-written state machine fits apps with few screens and a clear flow. Consider a navigation library when the app needs:
- Deep links from a notification or the web that open a screen several levels in.
- A long back stack with many branches, where users move freely between many screens.
- Screen transitions and per-screen saved state without building them yourself.
- Reopening the right screen after Android kills the app's process. The
StateFlowin this example lives in memory only, so the app returns to Welcome unless you save it yourself.
Navigation Compose has type-safe routes and deep links, and it runs on Compose Multiplatform. Switching to it later isn't hard either, because your Screen is already a typed list of screens.
Keep learning
Kotlin Mobile 101 teaches navigation with a screen state machine on a complete Kotlin Multiplatform app, with a unit test suite.