How do I use MainActivity to trigger this MainView...
# android
a
How do I use MainActivity to trigger this MainViewModel to run?
p
Do you mean constructor invocation? Did you instantiate it in
MainActivity
using e.g.
ViewModelProviders
factory?
👍 1
l
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 }