I am new to the mvvm pattern. I created a ViewModel for the main activity. Now I want to get an instance of the ViewModel in the main activity.
Most Tutorials and answe
Using Fragment-ktx libr in your app you can get viewModel as below
implementation 'androidx.fragment:fragment-ktx:1.1.0'
// get ViewModel in Activity or Fragment as
private val viewModel: MainActivityViewModel by viewModels()
// If you want to get same instance of ViewModel in ChildFragment as
private val viewModel: MainActivityViewModel by viewModels(
ownerProducer = { requireParentFragment() }
)