Compose Navigator provides possibility to Control AndroidX NavController from View Model's.
The library facilitates the usage of a single NavHost controlled by one point of truth - Navigator. Provides a unified interface for providing results back between composables and receiving activity results.
View Model's can use Navigator interface provided by navigation-runtime artifact to:
- navigate forward to
route
with optionalNavOptions
- navigate backward
- provide result data to previous backstack entry
- launch and receive activity results using
ActivityResultContract
Destination routes can be identified with String or Kotlin Serializable object used for Navigation Kotlin DSL Type-Safety.
Warning: library is designed to work with a single
NavHost
. Using nestedNavHost
is not tested and can cause unexpected behavior.
Compose Navigator is available via maven central.
- Add dependency to to
navigation-runtime
dependencies {
implementation "com.miquido.android.navigation:navigation-runtime:[version]"
}
- Choose dependency injection integration used in your project
navigation-hilt
ornavigation-koin
dependencies {
implementation "com.miquido.android.navigation:navigation-hilt:[version]"
// OR
implementation "com.miquido.android.navigation:navigation-koin:[version]"
}
Note: that for multi-module project add this dependency only in application module.
- Pass
NavController
toNavigationHandler
to start processing commands sent byNavigator
in View Models.
import com.miquido.android.navigation.handler.NavigationHandler
@kotlinx.serialization.Serializable
data object RootRoute
@kotlinx.serialization.Serializable
data object StartRoute
@Composable
fun AppRoot() {
// ...
val navController = rememberNavController()
NavigationHandler(
navController = navController
)
NavHost(
navController = navController,
route = RootRoute::class, // or simple string "root"
startDestination = StartRoute // or simple string "start"
) {
// build your NavGraph
}
}
- Create View Model in composable using
navEntryViewModel
method.
import com.miquido.android.navigation.Navigator
import com.miquido.android.navigation.viewmodel.navEntryViewModel
@Composable
fun StartScreen(
viewModel: StartViewModel = navEntryViewModel()
) {
}
Note: that all remaining dependency injection configuration (like using adding
@HiltViewModel
for Hilt or declaring module withviewModelOf
for Koin) remains same as it was.
- (Optional) Add integration with Compose Destinations
dependencies {
implementation "com.miquido.android.navigation:navigation-destinations:[version]"
}
Using this artifact provides additional kotlin extension methods for:
Navigator
to avoid callingDirection#rote
andRoute#route
when using provided API.NavAction
's to allow object creation usingDirection
/Route
directly.
The repository contains few samples - showcasing:
- koin integration sample-koin
- hilt integration sample-hilt
- TBD compose destination integration sample-destinations
The library contains a core feature set commonly used. Please do try it and open issues if you find any.
Any feedback and contributions are highly appreciated!
If you like the library, consider starring and sharing it with your colleagues.