Birgit L
04/08/2024, 11:49 AMorg.koin.core.error.KoinAppAlreadyStartedException: Trying to run new Koin Application whereas Koin is already started. Use 'KoinContext()' instead of check for any 'startKoin' usage.This can be reproduced by pressing back so the Activity goes into the background and is stopped, then returning to it. This crash has been discussed here before: https://kotlinlang.slack.com/archives/C67HDJZ2N/p1707810902616629 My solution to this for now is using an isolated context (docs), since I don't want to use platform specific solutions to starting koin from an application level, and having the koinApplication being created in an object prevents it from being recreated with the main composable, i.e. preventing the crash. Q1: Are there any downsides to this solution I should be aware of? Q2: Will there be an update to the docs or a bugfix so this crash does not happen anymore?
arnaud.giuliani
04/15/2024, 1:53 PMstartKoin
in your application class?arnaud.giuliani
04/15/2024, 1:54 PMstartKoin
and you should not need to recreate itBirgit L
04/15/2024, 1:55 PMarnaud.giuliani
04/15/2024, 1:56 PMKoinContext
composable then?Birgit L
04/15/2024, 2:00 PM@Composable
fun App() {
KoinIsolatedContext(context = MyIsolatedKoinContext.koinApp) {
AppContent()
}
}
@Composable
fun KoinIsolatedContext(
context: KoinApplication,
content: @Composable () -> Unit
) {
CompositionLocalProvider(
LocalKoinApplication provides context.koin,
LocalKoinScope provides context.koin.scopeRegistry.rootScope
) {
content()
}
}
arnaud.giuliani
04/15/2024, 2:22 PMKoinIsolatedContext
from Koin and even KoinContext
. This last won't throw exception on restart like KoinApplication
Birgit L
04/15/2024, 2:56 PM@Composable
fun App() {
KoinContext(context = koinApplication {
modules(sharedKoinModule)
}.koin) {
AppContent()
}
}