Skip to content

Instantly share code, notes, and snippets.

@lordcodes
Created March 1, 2020 20:04
Show Gist options
  • Save lordcodes/3bdfeb5486cc2eeda3e8bc4b7708c4b0 to your computer and use it in GitHub Desktop.
Save lordcodes/3bdfeb5486cc2eeda3e8bc4b7708c4b0 to your computer and use it in GitHub Desktop.
Code for the article: "The power of lazy properties in Kotlin"
inline fun <reified ViewModelT : ViewModel> ComponentActivity.bindViewModel() =
bindViewModel(ViewModelT::class, viewModelFactoryProvider)
@PublishedApi
internal fun <ViewModelT : ViewModel> ComponentActivity.bindViewModel(
viewModelType: KClass<ViewModelT>
): Lazy<ViewModelT> = lazy {
ViewModelProvider(this).get(viewModelType.java)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment