Skip to content

Instantly share code, notes, and snippets.

Last active December 5, 2019 15:05
What would you like to do?
Simple ViewModel Dagger Injection
class SomeFragment : DaggerFragment() {
lateinit var viewModelFactory: ViewModelFactory<SomeViewModel>
private val viewModel: SomeViewModel by viewModels { viewModelFactory }
lateinit var parentViewModelFactory: ViewModelFactory<ParentViewModel>
private val parentViewModel: ParentViewModel by viewModels(
{ requireActivity() },
{ parentViewModelFactory }
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import javax.inject.Inject
import javax.inject.Provider
class ViewModelFactory<T : ViewModel>
@Inject constructor(
private val provider: Provider<T>
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return provider.get() as T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment