InsertKoinIO / koin

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

ClosedScopeException on Device Rotation with koinInject #1773

Closed gecko10000 closed 8 months ago

gecko10000 commented 9 months ago

Describe the bug When using koinInject() in a Composable, rotating the device causes a ClosedScopeException and crashes the application.

To Reproduce Run this minimal reproducible example and rotate the device.

Expected behavior The rotation should succeed and the application should continue running.

Koin module and version: koin-android:3.5.3 koin-androidx-compose:3.5.3

arnaudgiuliani commented 9 months ago

Thanks for your feedback 👍

hoc081098 commented 9 months ago

@gecko10000 try wrapping the composable with KoinContext { ... }

gecko10000 commented 9 months ago

That fixes it, but it would be helpful to mention in the documentation, probably around here. I assumed androidContext(this@MainActivity) would be enough in the startKoin block (since it mostly worked).

arnaudgiuliani commented 9 months ago

Cool. I'll update the documentation 👍

arnaudgiuliani commented 8 months ago

Doc added 👍