Inside my Fragment I initialize a ViewModel using ViewModelProviders.
I want it to take its Activity if not null, otherwise itself (Fragment).
private val viewModel: MainViewModel by lazy {
ViewModelProviders.of(activity ?: this).get(MainViewModel::class.java)
}
None of the following functions can be called with the arguments supplied.
- of(Fragment) defined in androidx.lifecycle.ViewModelProviders
- of(FragmentActivity) defined in androidx.lifecycle.ViewModelPro
It seems the language does not allow me to invoke conflicting method signatures (between of(Activity) and of(Fragment). (It might be understandable, maybe the compiler has to reference only one method and cannot link to both on the same line.) Is that so?
I now have to use
activity?.let {
ViewModelProviders.of(it).get(MainViewModel::class.java)
} ?: run {
ViewModelProviders.of(this).get(MainViewModel::class.java)
}
Is there any better way of doing this?