In short: How can I inject a ViewModelProvider.Factory into my Fragment to get access to the appropriate ViewModel?
I have an activity with tabs managed by a BottomNavigationBar. Each tab screen is represented by a Fragment. Switching between tabs is done by replacing Fragments.
Each TabFragment has an associated ViewModel class. These ViewModel classes take arguments in the constructor, and for this reason I have to create a ViewModelProvider.Factory to use ViewModelProvider.
Naturally, my first instinct would be to inject this Factory into the fragment upon construction, and then retrieve a ViewModel instance via the ViewModelProvider:
class TabA(private val viewModelFactory: ViewModelProvider.Factory) : Fragment() {
...
tabAViewModel = ViewModelProvider(this, viewModelFactory)
.get(TabAViewmodel::class.java)
...
}
Fragments, however, require no-arguments constructors. Instead instantiation can be done through a static factory method and simple arguments can be supplied through a Bundle, but it seems that this Bundle cannot contain complex types such as a ViewModelFactory.
How can I then inject my ViewModelFactory into the Fragment? Is dependency injection of custom types even possible for fragments?