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

java.lang.IllegalStateException: Fragment Scope is already created #1433

Closed Zayankovsky closed 1 year ago

Zayankovsky commented 2 years ago

Describe the bug Koin crashes with the following stack trace:

java.lang.IllegalStateException: Fragment Scope is already created
    at org.koin.androidx.scope.FragmentExtKt.createFragmentScope(FragmentExt.kt:36)
    at org.koin.androidx.scope.ScopeFragment.onViewCreated(ScopeFragment.kt:46)
    at com.mapbox.onetap.map.presentation.ui.DropInFragment.onViewCreated(DropInFragment.kt:258)
    at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3128)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:552)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1881)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1814)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1751)
    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:538)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7590)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

To Reproduce Steps to reproduce the behavior:

  1. Replace fragment with another one and add transaction to back stack.
  2. Pop the last transaction.
  3. Koin crashes, because in this case fragment is not recreated (thus the scope still exists), but its view is. onViewCreated is called again and createFragmentScope crashes, because the scope is already created.

Expected behavior Koin should not crash if the scope is already created. Another option is to create the scope in onCreate instead of onViewCreated.

Koin project used and used version (please complete the following information): koin-android version 3.2.1. This does not happen with 3.2.0.

Pitel commented 2 years ago

This happens to me and I'm using ScopeFragment directly from Koin. :rage4:

alessandroToninelli commented 2 years ago

follow the issue

arnaudgiuliani commented 2 years ago

Replacing it doesn't close the source scope then. Did you try with koin-android 3.2.2?

Zayankovsky commented 1 year ago

Not reproducible with koin-android version 3.3.2.