When I use viewModels()
as below
class MainActivity : AppCompatActivity() {
private val viewModel: MainViewModel by viewModels()
// ... more codes
}
Why is it available in
implementation 'androidx.fragment:fragment-ktx:1.3.0'
Instead, when I include things like below, it is not available in them?
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
I thought it supposed to be in the viewmodel-ktx
library instead of fragment-ktx
library
As per the documentation, the by viewModels()
extension is an extension on the Fragment
class and therefore, must belong in the fragment-ktx
artifact - the lifecycle-viewmodel-ktx
and lifecycle-runtime-ktx
do not have any dependency on fragments and the lifecycle-extensions
artifact is completely deprecated and should not be used at all as per the Lifecycle 2.2.0 release notes.