You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When I use BottomNavigationView, two Fragments are used to display the page, and their ViewBinding both use Koin inject, but from Fragment A to Fragment B, and then from Fragment B to Fragment A, it throw can' t get Scope for lifecycleOwner
My Code
classMainActivity : BaseScopeActivity() {
....privateval homeFragment:HomeFragment by inject()
privateval mineFragment:MineFragment by inject()
funreplaceCurrentPage(type:Int) {
val targetFragment =if (type ==0) homeFragment else mineFragment
supportFragmentManager.beginTransaction()
.replace(R.id.mainContainer, targetFragment)
.commit()
}
....
}
solution
When I debug fragmentScope, I found that _scope of LifecycleScopeDelegate will be set to null when the fragment is replaced. But because the _scope of LifecycleScopeDelegate will not be reassigned, an exception is thrown.
At first, I tried this, but I found that getValue is called faster than onCreate
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
funonCreate(owner:LifecycleOwner) {
if (_scope!=null&&!_scope!!.closed) {
return
}
val internalScopeId = owner.getScopeId()
_scope= koin.getScopeOrNull(internalScopeId) ?: createScope(koin)
logger.debug("Recreate scope: $_scope for $owner")
}
Then I can only use this way
overridefungetValue(thisRef:LifecycleOwner, property:KProperty<*>): Scope {
// return _scope ?: error("can't get Scope for $lifecycleOwner")if (_scope!=null) return_scope!!val ownerState = thisRef.lifecycle.currentState
val ownerIsActive =
ownerState.isAtLeast(Lifecycle.State.CREATED)
if (!ownerIsActive) {
error("can't get Scope for $lifecycleOwner")
}
val koin = koinContext.get()
_scope= koin.getScopeOrNull(thisRef.getScopeId()) ?: createScope(koin)
return_scope!!
}
Koin project used and used version koin-android version 3.0.1 koin-android-ext version 3.0.1
Summary
I think this is because Koin has a problem with the lifecycle design of Fragment. Do you have any better solutions?
The text was updated successfully, but these errors were encountered:
Describe the bug
When I use BottomNavigationView, two Fragments are used to display the page, and their ViewBinding both use Koin inject, but from Fragment A to Fragment B, and then from Fragment B to Fragment A, it throw
can' t get Scope for lifecycleOwner
My Code
solution
When I debug fragmentScope, I found that _scope of LifecycleScopeDelegate will be set to null when the fragment is replaced. But because the _scope of LifecycleScopeDelegate will not be reassigned, an exception is thrown.
At first, I tried this, but I found that getValue is called faster than onCreate
Then I can only use this way
Koin project used and used version
koin-android version 3.0.1
koin-android-ext version 3.0.1
Summary
I think this is because Koin has a problem with the lifecycle design of Fragment. Do you have any better solutions?
The text was updated successfully, but these errors were encountered: