This is a sample Android application demonstrating the usage of Jetpack Navigation 3 (Nav3) with Jetpack Compose.
- Jetpack Navigation 3: Uses the latest experimental navigation library from Google.
- Type-Safe Navigation: Leverages Kotlin Serialization for type-safe navigation keys (
Home,Profile,FlowerDetail). - Jetpack Compose: Built entirely using modern Compose UI.
- Backstack Management: Demonstrates using
SnapshotStateListandmutableStateListOffor flexible, observable navigation state across multiple tabs. - Adaptive Layouts: Implements a custom
SceneStrategyfor List-Detail views on large screens.
MainActivity.kt: Contains the entry point and navigation logic usingNavDisplay.AppNavBackStack.kt: Manages multiple tab-specific backstacks using observable state.NavKey: Navigation destinations are defined as@Serializabledata objects.EntryProvider: Uses theentryProviderDSL to map navigation keys to Composable screens.SceneStrategy: Custom strategy (ListDetailSceneStrategy) to handle adaptive multi-pane layouts.
The project utilizes rememberSaveableStateHolderNavEntryDecorator and rememberViewModelStoreNavEntryDecorator to ensure that screen state is saved and ViewModels are properly scoped to their respective navigation entries.
By leveraging the SceneStrategy API, the app can display two active destinations simultaneously (e.g., a list and a detail view) when sufficient screen width is available, while automatically reverting to a single-pane flow on smaller devices.
- Persistent: Make it survivable through configuration changes and system-initiated process death
- CompositionLocal for Backstack: Implement a custom
NavEntryDecoratorto provide the backstack viaCompositionLocal, eliminating prop-drilling for navigation actions in deep hierarchies. - Synthetic Backstacks for Deep Linking: Develop a utility to parse incoming URLs/Intents and generate logical backstack sequences (e.g.,
[Home, Profile]instead of just[Profile]) to ensure intuitive back navigation. - Metadata-Driven Transitions: Utilize the Metadata DSL (anticipated in version 1.1) to define type-safe, declarative animations per
NavKeytype. - Kotlin Multiplatform (KMP) Readiness: Move core navigation logic and
NavKeydefinitions to a common KMP module to enable shared navigation state across Android, iOS, and Desktop.
- Ensure you have Android Studio installed.
- The project uses Kotlin 2.1.0 and Jetpack Compose.
- It requires the
kotlin-serializationplugin to be applied inbuild.gradle.kts.
Key dependencies include:
androidx.navigation3:navigation3-uiandroidx.navigation3:navigation3-runtimeandroidx.lifecycle:lifecycle-viewmodel-navigation3androidx.compose.material3.adaptive:adaptiveorg.jetbrains.kotlinx:kotlinx-serialization-json