Created
April 22, 2019 20:03
-
-
Save daverix/e1b2a956aebf502ebdd165a11b57b81a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.daverix.util | |
import androidx.fragment.app.FragmentActivity | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.ViewModelProvider | |
import androidx.lifecycle.ViewModelProviders | |
inline fun <reified T : ViewModel> FragmentActivity.getViewModel(crossinline factory: ()->T): T { | |
return ViewModelProviders.of(this, object : ViewModelProvider.Factory { | |
@Suppress("UNCHECKED_CAST") | |
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | |
return factory() as T | |
} | |
}).get(T::class.java) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment