From 353a3e52e603b50f933cd0f5c234e9bbfaa3d91a Mon Sep 17 00:00:00 2001 From: Sven Bendel Date: Wed, 5 Dec 2018 14:12:17 +0100 Subject: [PATCH] SFW-295 Fix dagger always returning the same view model instance, return a new one instead as ViewModelProviders is taking care of this for us when we want it --- di/README.md | 1 - .../io/stanwood/framework/arch/di/factory/ViewModelFactory.kt | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/di/README.md b/di/README.md index f563972..97f8c54 100644 --- a/di/README.md +++ b/di/README.md @@ -132,7 +132,6 @@ class HomeFragment : Fragment(), HasSupportFragmentInjector { There is not much to do for ViewModels except for adding a constructor annotated with `@Inject`: ```kotlin -@FragmentScope class HomeViewModel @Inject constructor(/* inject properties here if you want */) : ViewModel() {} ``` diff --git a/di/src/main/java/io/stanwood/framework/arch/di/factory/ViewModelFactory.kt b/di/src/main/java/io/stanwood/framework/arch/di/factory/ViewModelFactory.kt index abb8484..88eeec1 100644 --- a/di/src/main/java/io/stanwood/framework/arch/di/factory/ViewModelFactory.kt +++ b/di/src/main/java/io/stanwood/framework/arch/di/factory/ViewModelFactory.kt @@ -23,11 +23,11 @@ package io.stanwood.framework.arch.di.factory import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider -import dagger.Lazy import javax.inject.Inject +import javax.inject.Provider class ViewModelFactory @Inject constructor( - private val viewModel: Lazy + private val viewModel: Provider ) : ViewModelProvider.Factory { @Suppress("UNCHECKED_CAST") override fun create(modelClass: Class): T = viewModel.get() as T