Pedro Francisco de Sousa Neto
02/01/2023, 2:08 PMprivate val presenter: MyFeaturePresenter by scope.inject()
Sometimes I got the crash
Caused by: java.lang.IllegalStateException: Fragment MyFeatureFragment{e397a0b} (4be986d3-729a-430d-86ee-ee1f657be059) not attached to an activity.
at androidx.fragment.app.Fragment.requireActivity(Fragment.java:995)
at org.koin.androidx.scope.FragmentExtKt.createFragmentScope(FragmentExt.kt:38)
at org.koin.androidx.scope.FragmentExtKt$fragmentScope$1.invoke(FragmentExt.kt:50)
at org.koin.androidx.scope.FragmentExtKt$fragmentScope$1.invoke(FragmentExt.kt:50)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
Why?
Because this value val
is initializing in init
from Kotlin fragment class.
How to avoid that
private val presenter: MyFeaturePresenter by lazy { scope.get() }
Is it the expected scenario to Koin lazy injection? š¤Marcello Galhardo
02/01/2023, 5:37 PMrequireActivity
, after all the Fragment scope is a child from the Activity
(see scope.linkTo
in the code snippet you posted). Ignoring that would break the relationship between these two scopes.
Your proposed solution seems off. Arenāt inject
lazy by default? Wrapping it inside a lazy
should not change the behaviour. Check the last line of you stack trace SynchronizedLazyImpl.getValue
.
What do you mean by āval is initialized in initā? Do you have a code sample for this statement?arnaud.giuliani
02/06/2023, 10:56 AMarnaud.giuliani
02/06/2023, 10:57 AMScopeFragment
classPedro Francisco de Sousa Neto
02/06/2023, 1:59 PMScopeActivity
and ScopeFragment
.
I donāt know yet how can reproduce this error, but Iām getting him sometimes in Firebase Crashlytics.
My guess: sometimes fragment is initializing ātoo fastā and is not attached to activity yet, so requireActivity() throws an exception.arnaud.giuliani
02/06/2023, 5:30 PMPedro Francisco de Sousa Neto
02/06/2023, 5:43 PM3.3.1
, what version has it fixed?arnaud.giuliani
02/06/2023, 5:49 PMarnaud.giuliani
02/06/2023, 5:52 PMarnaud.giuliani
02/06/2023, 5:52 PMPedro Francisco de Sousa Neto
02/06/2023, 5:57 PMPedro Francisco de Sousa Neto
02/06/2023, 5:59 PMMarcello Galhardo
02/06/2023, 8:31 PMonCreate
and that is where your Fragment initialization logic should go to ensure your Fragment is in a safe state.
If you use lazy
, you should ensure it is only called "after" onCreated
state is reached or you likely will crash.Pedro Francisco de Sousa Neto
02/06/2023, 8:40 PMinject
in Koin usage is lazy and can handle this kind.
The problem is when you trigger a combination of createScope()->requireActivity()
because you just added a fragmentScope()
. And youāre not expecting that š
.Pedro Francisco de Sousa Neto
02/06/2023, 8:43 PMIs it the expected scenario to Koin lazy injection?I think itās not expected get a crash when youāre using
scope.injection
.
Maybe we need to improve docs, or annotate it with as throwable.
I really donāt know arnaud.giuliani
02/07/2023, 12:54 PMMaybe we need to improve docs, or annotate it with as throwable.yes documentation can be improved on this point š
Pedro Francisco de Sousa Neto
02/07/2023, 1:01 PMarnaud.giuliani
02/07/2023, 1:39 PM