Skip to content

Instantly share code, notes, and snippets.

@happysingh23828
Created April 6, 2022 08:19
Show Gist options
  • Save happysingh23828/93969a7bd8617b92bee882b54faffe06 to your computer and use it in GitHub Desktop.
Save happysingh23828/93969a7bd8617b92bee882b54faffe06 to your computer and use it in GitHub Desktop.
ViewModel Factory Extension
fun <T : ViewModel?> T.createFactory(): ViewModelProvider.Factory {
val viewModel = this
return object : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T = viewModel as T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment