https://kotlinlang.org logo
#koin-contributors
Title
# koin-contributors
p

Pedro Francisco de Sousa Neto

02/01/2023, 2:08 PM
When I use
Copy code
private val presenter: MyFeaturePresenter by scope.inject()
Sometimes I got the crash
Copy code
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
Copy code
private val presenter: MyFeaturePresenter by lazy { scope.get() }
Is it the expected scenario to Koin lazy injection? 🤔
m

Marcello Galhardo

02/01/2023, 5:37 PM
I’m not sure if Koin can avoid the
requireActivity
, 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?
a

arnaud.giuliani

02/06/2023, 10:56 AM
You should use `AndroidScopeComponent`interface. this way just use the i`nject()` function, directly with the right scope
also look at
ScopeFragment
class
p

Pedro Francisco de Sousa Neto

02/06/2023, 1:59 PM
I’m using
ScopeActivity
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.
a

arnaud.giuliani

02/06/2023, 5:30 PM
About ScopeFragment it was one problem we had, that scope couldn’t be attached. In latest version it’s now solved
p

Pedro Francisco de Sousa Neto

02/06/2023, 5:43 PM
hummm… I’m using
3.3.1
, what version has it fixed?
a

arnaud.giuliani

02/06/2023, 5:49 PM
you have it 😅
yes, your Activity is not yet attached
this mean you try to do something in a property that is initialized, before any onCreate call
p

Pedro Francisco de Sousa Neto

02/06/2023, 5:57 PM
Yes, that’s the problem! I’m doing something wrong in Koin usage? In my mind, that was a expected usage without crash, of course
I wrote an article in Medium platform about this error and how can avoid it. But, the doubt about usage continues. 🙃
m

Marcello Galhardo

02/06/2023, 8:31 PM
As a rule of thumb, there is no scenario in which you want to do any operation on a Fragment or Activity initialization time. Those components are created with an incomplete state, and only later will receive their respective contexts and required dependencies. That is why they offer you an
onCreate
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.
p

Pedro Francisco de Sousa Neto

02/06/2023, 8:40 PM
I agree totally with you @Marcello Galhardo. And
inject
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 😅.
Backing to my initial question...
Is 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 right know a better solution to help developers with Koin dev experience.
a

arnaud.giuliani

02/07/2023, 12:54 PM
Maybe we need to improve docs, or annotate it with as throwable.
yes documentation can be improved on this point 👍
p

Pedro Francisco de Sousa Neto

02/07/2023, 1:01 PM
Nice @arnaud.giuliani, I’ll do it ASAP! 😉
a

arnaud.giuliani

02/07/2023, 1:39 PM
great, thanks 🙂 Ping me on the issue/PR
31 Views