I’m trying to work out how to declare such a fragm...
# koin
m
I’m trying to work out how to declare such a fragment
ViewModel
in Koin:
Copy code
class FooFragment : Fragment() {

    val activityViewModel: BarViewModel by activityViewModel()

    val fragmentViewModel by viewModels<FooViewModel>(
        factoryProducer = {
            object : ViewModelProvider.Factory {
                override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T =
                    FooViewModel(
                        provider = activityViewModel.createSomeProvider(),
                        savedStateHandle = extras.createSavedStateHandle(),
                    ) as T
            }
        }
    )
}
The problem here is that I can’t declare the viewmodel in the usual way because it requires an argument from an activity viewmodel
a
what kind of argument? you can use parameter injection to help pass data to your definition if you need
m
Ah, I think the part I was missing that the declaration can just use
= get()
for the
savedStateHandle
(i.e. no need to use factory in order to obtain extras) and, yes, the provider can use parameter injection. Thanks!
👍 1
I’m curious if this is generally acceptable practice: to pass in an argument obtained from an activity view model, into a fragment view model?
a
not sure it's a great thing to chain injection via viewModel, I would try something else with Koin 🤔