Skip to content

Instantly share code, notes, and snippets.

@RoryKelly
Created June 26, 2017 11:01
Show Gist options
  • Save RoryKelly/e73e50a2716d9ec23a59581554080eda to your computer and use it in GitHub Desktop.
Save RoryKelly/e73e50a2716d9ec23a59581554080eda to your computer and use it in GitHub Desktop.
Neat function to bind ViewModels on android
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