Fixed
Status Update
Comments
ap...@google.com <ap...@google.com> #2
Project: platform/frameworks/support
Branch: androidx-main
commit 47bf66aaefba2f62eea8a4120318ad24bfd7ed08
Author: Jeremy Woods <jbwoods@google.com>
Date: Tue Jan 11 16:25:15 2022
Fix default factory for lazy viewModels
When calling by viewModels and only passing in an onwerProducer instead
of using the fragment's ViewModelProvider.Factory by default, we should
use the owner's factory. This will ensure the viewModels are created
with the proper state.
RelNote: "The by viewModels() lazy function will now have the proper
`ViewModelProvider.Factory` by default instead of using the fragment's
factory."
Test: adjsuted FragmentViewModelLazyTest
Bug: 214106513
Change-Id: I561707e21a66afb0540c941bd5b94f834dc3953c
M fragment/fragment-ktx/src/main/java/androidx/fragment/app/FragmentViewModelLazy.kt
M fragment/fragment-ktx/src/androidTest/java/androidx/fragment/app/FragmentViewModelLazyTest.kt
https://android-review.googlesource.com/1946014
Branch: androidx-main
commit 47bf66aaefba2f62eea8a4120318ad24bfd7ed08
Author: Jeremy Woods <jbwoods@google.com>
Date: Tue Jan 11 16:25:15 2022
Fix default factory for lazy viewModels
When calling by viewModels and only passing in an onwerProducer instead
of using the fragment's ViewModelProvider.Factory by default, we should
use the owner's factory. This will ensure the viewModels are created
with the proper state.
RelNote: "The by viewModels() lazy function will now have the proper
`ViewModelProvider.Factory` by default instead of using the fragment's
factory."
Test: adjsuted FragmentViewModelLazyTest
Bug: 214106513
Change-Id: I561707e21a66afb0540c941bd5b94f834dc3953c
M fragment/fragment-ktx/src/main/java/androidx/fragment/app/FragmentViewModelLazy.kt
M fragment/fragment-ktx/src/androidTest/java/androidx/fragment/app/FragmentViewModelLazyTest.kt
jb...@google.com <jb...@google.com> #3
This has been fixed internally and will be available in the Fragment 1.4.1
release.
Description
When calling the
by viewModels
lazy function from a fragment, if you passed in the owner and did not specify the factory i.e:the resulting viewModel would be created using the fragment's ViewModel factory instead of the owner's, which in this case is the activity's factory.
We should make sure that the
viewModels
function first uses the factory passed in, followed by the owner's, and if those are both null, only then does it fall back to the fragment's.