:kotlin:otlinize! <#C59B2DTHD|inlinereified> ``` ...
# android
m
Kotlinize! #inlinereified
Copy code
inline fun <reified T : ViewModel> FragmentActivity.getViewModel(key: String? = null): T {
    val provider = ViewModelProviders.of(this)
    return if (key != null) {
        provider.get(key, T::class.java)
    } else {
        provider.get(T::class.java)
    }
}
usage:
Copy code
userViewModel = getViewModel()
instead of:
Copy code
userViewModel = ViewModelProviders.of(this).get(UserViewModel::class.java)
­čĹŹ 1
K 4
s
mg6maciej:
Copy code
return ViewModelProviders.of(this)
.run {key?.let {get(it, T::class.java)} ?: get(T::class.java)}
m
This does not look
fun
. I'm a stupid person and like my code not to be smarter than me. ;)
s
Nice one­čść