Skip to content

Instantly share code, notes, and snippets.

@Elforama
Last active January 1, 2022 14:58
Show Gist options
  • Save Elforama/969c2de0b3227f927fbf3f65654acf63 to your computer and use it in GitHub Desktop.
Save Elforama/969c2de0b3227f927fbf3f65654acf63 to your computer and use it in GitHub Desktop.
Android view model factory implementation allowing easy view model constructor injection.
/**
* Usage example:
*
* @Inject
* lateinit var mFactory: VMInjectionFactory<MyViewModel>
*/
class VMInjectionFactory <out Type: ViewModel> @Inject constructor(val vm: Lazy<Type>): ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(p0: Class<T>): T {
return vm.get() as T
}
}
@ericntd
Copy link

ericntd commented Jan 1, 2022

Do you know what's the difference betweenLazy<> and Provider<> wrapper here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment