Skip to content

Instantly share code, notes, and snippets.

@polson
Created May 22, 2019 17:17
Show Gist options
  • Save polson/7f8d9e13ab8573c8e48c90e73efef4b6 to your computer and use it in GitHub Desktop.
Save polson/7f8d9e13ab8573c8e48c90e73efef4b6 to your computer and use it in GitHub Desktop.
@Suppress("UNCHECKED_CAST")
private inline fun <reified T : ViewModel> createViewModel(key: String, crossinline initializer: () -> T): T {
val factory = object : ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel?> create(modelClass: Class<T>): T = initializer() as T
}
return ViewModelProviders.of(activity, factory).get(key, T::class.java)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment