InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.98k stars 710 forks source link

KoinAppAlreadyStartedException after changing theme #1840

Closed Cuyer closed 2 weeks ago

Cuyer commented 5 months ago

Describe the bug When changing from light to dark theme or vice versa, following error is thrown:

KoinAppAlreadyStartedException: A Koin Application has already been started

To Reproduce Steps to reproduce the behavior:

  1. Change theme to from light mode to dark mode or vice versa
  2. App crashes

Expected behavior App doesn't crash

Koin module and version: I suppose its koin-compose:3.6.0-wasm-alpha2

I am working on a project that uses compose multiplatform. I've tried using documentation to start koin, but its lacking information regarding compose multiplatform.

Snippet or Sample project to help reproduce

class MainActivity : FragmentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        this.enableEdgeToEdge()
        super.onCreate(savedInstanceState)
        startKoin {
            androidContext(this@MainActivity)
            modules(
                appModule(),
                androidModule()
            )
            logger(PrintLogger())
        }
        setContent {
            AppTheme {
                App()
            }
        }

    }
}
gamer3dx commented 5 months ago

You initializing Koin inside onCreate in Activity, which is recreated on theme change. Try to initialize it inside onCreate in Application.

Cuyer commented 5 months ago

I've tried it as such:

class MainApp : Application() {

    override fun onCreate() {
        super.onCreate()

        startKoin {
            androidContext(this@MainApp)
            modules(
                appModule(),
                androidModule()
            )
            logger(PrintLogger())
        }
    }
}

But I get error java.lang.IllegalStateException: KoinApplication has not been started

gamer3dx commented 5 months ago

Interesting. What are you doing to get this exception? I remember that I encountered this error on first inject call in my pet project when I forgot to register my application class in Manifest file.

Cuyer commented 5 months ago

Yes, that's exactly the issue. I had to register application class in manifest file and everything works now. And I've checked and after chaning theme the app doesn't crash. Thank You!

kopoh commented 5 months ago

Omg I have the same problem, I do initKoin() in Common Main:

@Composable
internal fun App() {
    AppTheme {
        initKoin()
        Napier.base(DebugAntilog())

        Navigator(screen = FirstScreen)
    }
}
fun initKoin() = startKoin {
    modules(
        sharedViewModelModule,
        LoginViewModelModule,
        SettingsValueModule,
    )
}

and my application crashes already when switching themes using the theme switch button in the status bar.

stale[bot] commented 3 weeks ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.