Reputation: 313
We can initialize viewModel either by
private val viewModel: XViewModel by viewModels()
or
viewModel = ViewModelProvider(this).get(XViewModel::class.java)
in a Fragment.
I want to know if
private val viewModel: XViewModel by viewModels()
can be used in an Activity as well.
Over here, I realized is related to navigation and fragment.
Upvotes: 2
Views: 1411
Reputation: 95578
You are missing this import in your Activity
:
import androidx.activity.viewModels
Upvotes: 3