question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Crash during Fragment replace for scoped ViewModel

See original GitHub issue

Describe 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:

  1. Install the demo app
  2. Click on ‘Replace fragment’
  3. Then click to button once again
  4. 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
egorikftpcommented, Jun 25, 2019

@ShwetaChauhan18 Thanks for your solution. I see that it works on the sample project, but the solution looks strange)

I’ll wait official fix.

2reactions
MehulKKcommented, Jun 24, 2019

@ShwetaChauhan18 : thanks solve this issue.

@egorikftp : you can apply this solution. its solve at my end.

Read more comments on GitHub >

github_iconTop Results From Across the Web

App Crash once replace Fragment for scoped ViewModel
I was found an issue when I try to open fragment and then replace with the same fragment. In our prod application, it's...
Read more >
App Crash once replace Fragment for scoped ViewModel-kotlin
[Solved]-App Crash once replace Fragment for scoped ViewModel-kotlin. Search. score:4 ... First in your module class create scope by named koin method.
Read more >
ViewModel Scoping APIs - Android Developers
You can scope a ViewModel to an Activity, Fragment, or destination of a Navigation graph. The viewModels() extension functions provided by the Activity, ......
Read more >
5 common mistakes when using Architecture Components
Leaking LiveData observers in Fragments ... example leak can be passing in ViewModel a listener to Repository, which is Singleton scoped, ...
Read more >
Safe delay in Android Views: goodbye Handlers, Hello ...
According to the documentation, you can use an Executor or access the Handler of a view as a replacement, but they can also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found