Repository navigation
Replies: 1 comment
|
The reconfiguration problem happens because You said you don't want extra ViewModels, but // One ViewModel per scope boundary — owns the scope lifecycle
class ModuleScopeViewModel : ViewModel(), KoinScopeComponent {
override val scope: Scope = createScope<ModuleScopeViewModel>()
override fun onCleared() = scope.close()
}Your graph, rewritten to survive reconfigurations: @Composable
fun SomeModuleGraph() {
val navController = rememberNavController()
// ViewModel survives config changes — scope stays alive
val moduleVm: ModuleScopeViewModel = koinViewModel()
// Expose the stable scope to children
KoinScope(scope = moduleVm.scope) {
NavHost(navController, startDestination = "screen1") {
composable("screen1") {
// Screen-level dependencies resolved from the parent scope
val dep = moduleVm.scope.get<Screen1Dep>()
Screen1Route(dep)
}
composable("screen2") {
val dep = moduleVm.scope.get<Screen2Dep>()
Screen2Route(dep)
}
}
}
}If you truly want no ViewModel at all, use @Composable
fun SomeModuleGraph() {
val scopeId = rememberSaveable { uuid4().toString() }
val scope = remember(scopeId) {
getKoin().getOrCreateScope(scopeId, named<SomeModule>())
}
DisposableEffect(scope) {
onDispose { scope.close() }
}
KoinScope(scope = scope) {
// NavHost here
}
}
The |
Uh oh!
There was an error while loading. Please reload this page.
Lets consider I have this:
My custom contexts:
But this solution works bad on reconfiguration because KoinScopeComponent recreates. So it is needed to use ScopeViewModel instead of KoinScopeComponent but what if I don't want to create new ViewModel. Also are there any solutions to make it without creating root route with navhost?
All reactions