Crash during Fragment replace for scoped ViewModel
See original GitHub issueDescribe the bug I was found an issue when I try to open fragment and then replace with the same fragment. In our prod application, it’s a popular case.
java.lang.IllegalStateException: Definition without any InstanceContext - [type:Factory,scope:'com.egoriku.testapplication.fragment.CrashFragment', primary_type:'com.egoriku.testapplication.viewmodel.TestViewModel'] at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:71) at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165) at org.koin.core.scope.Scope.get(Scope.kt:128) at org.koin.androidx.viewmodel.ViewModelResolutionKt$createViewModelProvider$1.create(ViewModelResolution.kt:66) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103) at org.koin.androidx.viewmodel.ViewModelResolutionKt.getInstance(ViewModelResolution.kt:43) at org.koin.androidx.viewmodel.ViewModelScopeResolutionKt.getViewModel(ViewModelScopeResolution.kt:14) at org.koin.androidx.viewmodel.ext.android.ScopeExtKt.getViewModel(ScopeExt.kt:86) at com.egoriku.testapplication.fragment.CrashFragment$$special$$inlined$viewModel$1.invoke(ScopeExt.kt:96) at com.egoriku.testapplication.fragment.CrashFragment$$special$$inlined$viewModel$1.invoke(ScopeExt.kt) at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) at com.egoriku.testapplication.fragment.CrashFragment.getModel(CrashFragment.kt) at com.egoriku.testapplication.fragment.CrashFragment.onViewCreated(CrashFragment.kt:26)
To Reproduce I have prepared a sample demo project with a crash.
https://github.com/egorikftp/KoinBug
Steps to reproduce the behaviour:
- Install the demo app
- Click on ‘Replace fragment’
- Then click to button once again
- See error
Expected behaviour New fragment instance created without a crash.
Koin project used and used version:
koin version 2.0.1
Note
There is no crash in case I use add
fragment in the sample.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:9 (1 by maintainers)
@ShwetaChauhan18 Thanks for your solution. I see that it works on the sample project, but the solution looks strange)
I’ll wait official fix.
@ShwetaChauhan18 : thanks solve this issue.
@egorikftp : you can apply this solution. its solve at my end.