https://kotlinlang.org logo
Title
h

HarryPulvirenti

06/05/2020, 8:50 AM
Hi Everyone, I'm facing a bug related to the
koin
implementation of the
FragmentFactory
. Basically, if you inject a
ViewModel
in the constructor of the
Fragment
, this one will be instantiated again if you rotate the screen. This bug is reproducible only injecting the
ViewModel
via the fragment constructor but is not possible to reproduce it injecting the
ViewModel
via the lazy extension
viewModel()
Let me know if you are already aware of this bug or I can open an issue on Github.
t

tynn

06/05/2020, 7:35 PM
If you use
get()
to obtain your view model within your fragment factory, it’s the expected behavior.
If you don’t use the fragment itself as the
ViewModelStoreOwner
to obtain the view model, you could use
ViewModelStoreOwner.getViewModel<Model>()
instead.
a

arnaud.giuliani

06/09/2020, 9:08 AM
I agree here, we need a ViewModelStoreOwner to inject a ViewModel instance the right way
h

HarryPulvirenti

06/10/2020, 8:51 AM
Sorry I just noticed the comments. So I have to create a
ViewModelStoreOwner
in some way and then use it to inject the
ViewModel
?
a

arnaud.giuliani

06/10/2020, 9:18 AM
A Fragment is a
ViewModelStoreOwner
the thing is trying to inject it by constructor
don’t know how the ViewModelFactory can be triggered from the constructor, need to check a bit