Created
June 26, 2017 11:01
-
-
Save RoryKelly/e73e50a2716d9ec23a59581554080eda to your computer and use it in GitHub Desktop.
Neat function to bind ViewModels on android
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
class StartRecordingActivity : ToolbarActivity() { | |
private val startRecordingViewModel: StartRecordingViewModel by bindViewModel(this, StartRecordingViewModel::class) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
/* your ui code */ | |
} | |
} | |
// put this in utils somewhere | |
fun <T : ViewModel, S : FragmentActivity> bindViewModel(to: S, modelClass: KClass<T>) = lazy { | |
ViewModelProviders.of(to).get(modelClass.java) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment