compose-navigation3
Skill8 starsChanged 4 months agoApache-2.0View on GitHub
Implements type-safe navigation using Jetpack Navigation 3 (Nav3) with Compose. Use when user asks to "add a new screen", "wire navigation", "pass results between screens", "set up Nav3", or "navigate to a destination". Covers NavKey routes, NavDisplay, navigator abstractions, and ResultStore.
In this repository14 other files it gives its agents.
- android-modularizationSkill
- android-permissions-editorSkill
- android-unit-test-editorSkill
- compose-editorSkill
- compose-performance-auditorSkill
- github-action-editorSkill
- gradle-configurationSkill
- gradle-convention-pluginSkill
- koin-editorSkill
- kotlin-conventionSkill
- kotlin-coroutinesSkill
- mvi-editorSkill
- paparazzi-editorSkill
- review-owasp-securitySkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 46
Every destination is a @Serializable Kotlin type implementing NavKey.
- Very specific: line 70
Create a thin wrapper around MutableBackStack<NavKey> for each NavDisplay level.
- Very specific: line 216
Use noAnimationMetadata() for tab roots, horizontalSlideMetadata() for drill-down screens, and verticalSlideMetadata() for full-screen …
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Compose Navigation 3
Overview
Jetpack Navigation 3 (Nav3) replaces the NavController / NavGraphBuilder model with a state-first API:
| Nav2 concept | Nav3 replacement |
NavController | Custom navigator class backed by a MutableBackStack<NavKey> |
NavGraphBuilder + composable<> | EntryProviderScope<NavKey> + entry<> |
NavHost | NavDisplay |
Bundle arguments | @Serializable data class implementing NavKey |
savedStateHandle result | ResultStore + CompositionLocal |
Core rule: ALL navigation logic lives in the app module. Feature modules expose Screen composables only — they have no dependency on Nav3 types.
Directory Structure
app/src/main/java/<package>/navigation/
├── root/
│ ├── RootNavigation.kt # Root NavDisplay — full-screen flows
│ └── RootNavigator.kt # navigate(), popBackStack(), replaceTop()
├── bottombar/
│ ├── BottomBarNavigation.kt # BottomBar NavDisplay — tab sub-screens
│ └── BottomBarNavigator.kt # goBack(), navigate(), reSelectTab()
├── result/
│ └── ResultStore.kt # ResultStore class + LocalResultStore CompositionLocal
├── utils/
│ └── NavigationTransitions.kt # Transition specs and metadata helpers
└── [feature]/
└── [Feature]Navigation.kt # Entry function + NavKey route (+ ResultKey if consumer)1. NavKey Routes
Every destination is a @Serializable Kotlin type implementing NavKey.
// Object route — no parameters
@Serializable
object HomeRoute : NavKey
// Data class route — type-safe parameters, no bundles
@Serializable
data class ProductDetailRoute(val productId: String) : NavKey
// Navigate
navigator.navigate(ProductDetailRoute(productId = "abc-123"))
// Receive in entry — parameters are directly on the key
entry<ProductDetailRoute> { key ->
ProductDetailScreen(productId = key.productId, ...)
}2. Navigator
Create a thin wrapper around MutableBackStack<NavKey> for each NavDisplay level.
// RootNavigator.kt
class RootNavigator(private val backStack: MutableBackStack<NavKey>) {
fun navigate(route: NavKey) { backStack.add(route) }
fun popBackStack() { backStack.removeLastOrNull() }
fun replaceTop(route: NavKey) {
backStack.removeLastOrNull()
backStack.add(route)
}
}
// BottomBarNavigator.kt
class BottomBarNavigator(private val backStack: MutableBackStack<NavKey>) {
fun navigate(route: NavKey) { backStack.add(route) }
fun goBack() { backStack.removeLastOrNull() }
fun reSelectTab(tab: NavKey) {
backStack.removeAll { it == tab }
backStack.add(tab)
}
}3. NavDisplay Setup
// RootNavigation.kt
@Composable
fun RootNavigation() {
val backStack = rememberMutableBackStack<NavKey>(listOf(HomeRoute))
val resultStore = remember { ResultStore() }
val rootNavigator = remember { RootNavigator(backStack) }
CompositionLocalProvider(LocalResultStore provides resultStore) {
NavDisplay(
backStack = backStack,
entryProvider = entryProvider {
homeEntry(rootNavigator)
productDetailEntry(rootNavigator)
// register all full-screen entries here
}
)
}
}4. Entry Patterns
Basic Entry
fun EntryProviderScope<NavKey>.productDetailEntry(rootNavigator: RootNavigator) {
entry<ProductDetailRoute>(metadata = horizontalSlideMetadata()) {
val viewModel: ProductDetailViewModel = koinViewModel()
val state by viewModel.viewState.collectAsStateWithLifecycle()
ProductDetailScreen(
state = state,
onEvent = viewModel::onViewEvent,
sideEffects = viewModel.sideEffect,
onNavigation = { effect ->
when (effect) {
ProductDetailSideEffect.Navigation.Back ->
rootNavigator.popBackStack()
is ProductDetailSideEffect.Navigation.OpenRelated ->
rootNavigator.navigate(ProductDetailRoute(effect.productId))
}
},
)
}
}
@Serializable
data class ProductDetailRoute(val productId: String) : NavKeyTab Root Screen (no animation)
entry<BottomTab.Home>(metadata = noAnimationMetadata()) {
val viewModel: HomeViewModel = koinViewModel()
val state by viewModel.viewState.collectAsStateWithLifecycle()
HomeScreen(state = state, onEvent = viewModel::onViewEvent, ...)
}BottomBar Sub-Screen
fun EntryProviderScope<NavKey>.settingsEntry(navigator: BottomBarNavigator) {
entry<SettingsRoute>(metadata = horizontalSlideMetadata()) {
val viewModel: SettingsViewModel = koinViewModel()
val state by viewModel.viewState.collectAsStateWithLifecycle()
SettingsScreen(
state = state,
onEvent = viewModel::onViewEvent,
sideEffects = viewModel.sideEffect,
onNavigation = { effect ->
when (effect) {
SettingsSideEffect.Navigation.Back -> navigator.goBack()
}
},
)
}
}
@Serializable
object SettingsRoute : NavKeySub-Screen That Also Launches Full-Screen Flow
fun EntryProviderScope<NavKey>.profileEntry(
rootNavigator: RootNavigator,
navigator: BottomBarNavigator,
) {
entry<ProfileRoute>(metadata = horizontalSlideMetadata()) {
// ...
onNavigation = { effect ->
when (effect) {
ProfileSideEffect.Navigation.Back -> navigator.goBack()
ProfileSideEffect.Navigation.OpenSetup -> rootNavigator.navigate(SetupRoute)
}
}
}
}5. Transition Helpers
// NavigationTransitions.kt
fun horizontalSlideMetadata(): NavEntryMetadata = /* slide in from end, slide out to end */
fun noAnimationMetadata(): NavEntryMetadata = /* no transition, instant switch */
fun verticalSlideMetadata(): NavEntryMetadata = /* slide up from bottom */Use noAnimationMetadata() for tab roots, horizontalSlideMetadata() for drill-down screens, and verticalSlideMetadata() for full-screen modal flows (e.g., onboarding, setup wizards).
6. ResultStore — Cross-Screen Results
ResultStore passes data back from a producer screen to its consumer without coupling them via parameters.
Signal (no data)
// --- Consumer (CheckoutNavigation.kt) ---
fun EntryProviderScope<NavKey>.checkoutEntry(rootNavigator: RootNavigator) {
entry<CheckoutRoute> {
val resultStore = LocalResultStore.current
val paymentDone = resultStore.getResult<PaymentCompleted>(PaymentCompletedKey)
LaunchedEffect(paymentDone) {
if (paymentDone != null) {
resultStore.removeResult(PaymentCompletedKey)
rootNavigator.popBackStack()
}
}
// ...
}
}
data object PaymentCompleted
object PaymentCompletedKey
// --- Producer (PaymentNavigation.kt) ---
onNavigation = { effect ->
when (effect) {
PaymentSideEffect.Navigation.Success -> {
resultStore.setResult(PaymentCompletedKey, PaymentCompleted)
rootNavigator.popBackStack()
}
}
}Data Payload
// Consumer (OrderNavigation.kt)
val result = resultStore.getResult<AddressResult>(AddressResultKey)
LaunchedEffect(result) {
result?.let {
viewModel.onViewEvent(OrderViewEvent.AddressSelected(it.addressId))
resultStore.removeResult(AddressResultKey)
}
}
data class AddressResult(val addressId: String)
object AddressResultKeyMultiple Result Types (sealed interface)
// Consumer (FeedNavigation.kt)
val scrollRequest = resultStore.getResult<FeedScrollRequest>(FeedScrollRequestKey)
LaunchedEffect(scrollRequest) {
if (scrollRequest != null) {
resultStore.removeResult(FeedScrollRequestKey)
when (scrollRequest) {
is FeedScrollRequest.ToItem ->
viewModel.onViewEvent(FeedViewEvent.ScrollToItem(scrollRequest.itemId))
FeedScrollRequest.ResetToTop ->
viewModel.onViewEvent(FeedViewEvent.ResetToTop)
}
}
}
sealed interface FeedScrollRequest {
data class ToItem(val itemId: String) : FeedScrollRequest
data object ResetToTop : FeedScrollRequest
}
object FeedScrollRequestKeyResultStore Implementation
// ResultStore.kt
class ResultStore {
private val results = mutableStateMapOf<Any, Any?>()
fun <T : Any> setResult(key: Any, value: T) { results[key] = value }
fun <T : Any> getResult(key: Any): T? = results[key] as? T
fun removeResult(key: Any) { results.remove(key) }
}
val LocalResultStore = staticCompositionLocalOf<ResultStore> {
error("No ResultStore provided")
}Do / Don't
| ✅ Do | ❌ Don't |
Put navigation wiring in app/navigation/[feature]/ | Put navigation code in feature/, component/, or common/ modules |
Use @Serializable data class/object routes implementing NavKey | Use string routes or Bundle arguments |
Access ResultStore via LocalResultStore.current inside entry lambda | Pass ResultStore as a function parameter |
Use koinViewModel() (or equivalent DI) in entries | Instantiate ViewModels manually |
Emit Navigation.* side effects from ViewModels | Call navigator methods from composables directly |
Use noAnimationMetadata() for tab roots | Animate tab root switches |
Use horizontalSlideMetadata() for drill-down screens | Mix transition styles inconsistently |
Handle back via Navigation.Back side effect | Use BackHandler in screen composables |
Use remember { ResultStore() } — plain remember | Use rememberSaveable { ResultStore() } — crashes, not Bundle-serializable |
Register each route in exactly one NavDisplay | Register the same route in multiple NavDisplays |
Verification Checklist
- Route is
@Serializableand implementsNavKey - Entry registered in exactly one NavDisplay
- ViewModel resolved with
koinViewModel()(or your DI) - No
BackHandlerin the Screen composable - Correct animation metadata applied (
noAnimationMetadatavshorizontalSlideMetadata) ResultStoreaccessed viaLocalResultStore.current(never as parameter)ResultKey+ payload type defined in the consumer file- Producer imports
ResultKeyfrom the consumer file - No navigation logic in any module other than
app ResultStorecreated withremember { }, notrememberSaveable { }
References
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

