Skip to content

Instantly share code, notes, and snippets.

@coroutineDispatcher
Last active March 31, 2020 17:56
Show Gist options
  • Save coroutineDispatcher/2b9de0cd9d9d6b6c8b4932b0b6114901 to your computer and use it in GitHub Desktop.
Save coroutineDispatcher/2b9de0cd9d9d6b6c8b4932b0b6114901 to your computer and use it in GitHub Desktop.
inline fun <reified T : ViewModel> Fragment.viewModel(
crossinline provider: (SavedStateHandle) -> T
) = viewModels<T> {
object : AbstractSavedStateViewModelFactory(this, fragment.arguments ?: Bundle()) {
override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T =
provider(handle) as T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment