为什么 Android viewModels() 扩展函数在 fragment-ktx 而不是 viewmodel-ktx 库中可用?
Why is the Android viewModels() extension function available in the fragment-ktx instead of viewmodel-ktx library?
当我如下使用viewModels()
时
class MainActivity : AppCompatActivity() {
private val viewModel: MainViewModel by viewModels()
// ... more codes
}
为什么它在
中可用
implementation 'androidx.fragment:fragment-ktx:1.3.0'
相反,当我包含如下内容时,它们不可用?
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'
我认为它应该在 viewmodel-ktx
库而不是 fragment-ktx
库
根据 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.
当我如下使用viewModels()
时
class MainActivity : AppCompatActivity() {
private val viewModel: MainViewModel by viewModels()
// ... more codes
}
为什么它在
中可用implementation 'androidx.fragment:fragment-ktx:1.3.0'
相反,当我包含如下内容时,它们不可用?
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'
我认为它应该在 viewmodel-ktx
库而不是 fragment-ktx
库
根据 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.