https://kotlinlang.org logo
#android
Title
# android
a

Aiden

11/15/2019, 6:58 AM
How do I use MainActivity to trigger this MainViewModel to run?
p

pawegio

11/15/2019, 8:39 AM
Do you mean constructor invocation? Did you instantiate it in
MainActivity
using e.g.
ViewModelProviders
factory?
👍 1
l

Lebohang

11/25/2019, 1:58 PM
On the MainActivity you will create a variable for your viewmodel e.g
private lateinit var viewmodel
within the
onCreate()
method you will instantiate the viewmodel in the following format
viewmodel = ViewModelProviders.of(this, factory).get(MainViewModel::class.java)
,
factory
represent the factory for you view model. But you can also do it the following way if you are using new android version:
private val viewModel by viewModels<MainViewModel> { factory }
2 Views