Fixed
Status Update
Comments
ap...@google.com <ap...@google.com> #2
Project: platform/frameworks/support
Branch: androidx-main
commit 01fa21aa0023a79b6c231bfa1e41081afc02dc9c
Author: Jeremy Woods <jbwoods@google.com>
Date: Wed Mar 09 13:25:57 2022
Prioritize CreationExtras for ViewModelFactories
We currently have a hard separation between the new stateless
ViewModelFactory that supports CreationExtras and the old stateful
factory that does not. While this was intended, this can cause problems
for compat with older versions, specifically Hilt, which makes a direct
call to the factory create function.
For a compat approach, we can instead allow using CreationExtras when
using the stateful constructor, preferring CreationExtras over any state
that was passed in. This way if there are components that have yet to
integrate CreationExtras, they can still work with newer components that
have.
RelNote: "`SavedStateViewFactory` now supports using `CreationExtras`
even when it was initialized with a `SavedStateRegistryOwner`. If extras
are provided, the initialized arguments are ignored."
Test: SavedStateFactoryTest
Bug: 224844583
Change-Id: I6c43bfd75888cb4b8bdd610cd07d4962aaba37ea
M lifecycle/lifecycle-viewmodel-savedstate/src/main/java/androidx/lifecycle/SavedStateViewModelFactory.kt
M activity/activity/src/main/java/androidx/activity/ComponentActivity.java
M fragment/fragment/src/main/java/androidx/fragment/app/Fragment.java
M lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/SavedStateFactoryTest.kt
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentViewLifecycleOwner.java
M navigation/navigation-common/src/main/java/androidx/navigation/NavBackStackEntry.kt
https://android-review.googlesource.com/2019219
Branch: androidx-main
commit 01fa21aa0023a79b6c231bfa1e41081afc02dc9c
Author: Jeremy Woods <jbwoods@google.com>
Date: Wed Mar 09 13:25:57 2022
Prioritize CreationExtras for ViewModelFactories
We currently have a hard separation between the new stateless
ViewModelFactory that supports CreationExtras and the old stateful
factory that does not. While this was intended, this can cause problems
for compat with older versions, specifically Hilt, which makes a direct
call to the factory create function.
For a compat approach, we can instead allow using CreationExtras when
using the stateful constructor, preferring CreationExtras over any state
that was passed in. This way if there are components that have yet to
integrate CreationExtras, they can still work with newer components that
have.
RelNote: "`SavedStateViewFactory` now supports using `CreationExtras`
even when it was initialized with a `SavedStateRegistryOwner`. If extras
are provided, the initialized arguments are ignored."
Test: SavedStateFactoryTest
Bug: 224844583
Change-Id: I6c43bfd75888cb4b8bdd610cd07d4962aaba37ea
M lifecycle/lifecycle-viewmodel-savedstate/src/main/java/androidx/lifecycle/SavedStateViewModelFactory.kt
M activity/activity/src/main/java/androidx/activity/ComponentActivity.java
M fragment/fragment/src/main/java/androidx/fragment/app/Fragment.java
M lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/SavedStateFactoryTest.kt
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentViewLifecycleOwner.java
M navigation/navigation-common/src/main/java/androidx/navigation/NavBackStackEntry.kt
jb...@google.com <jb...@google.com> #3
This has been fixed internally and will be in the Lifecycle 2.5.0-alpha05
release.
Activity 1.5.0-alpha04
, Fragment 1.5.0-alpha04
, and Navigation 2.5.0-alpha04
will all contain this change.
Description
Now that we have
CreationExtras
, we offer a stateless version of severalViewModelProvider.Factory
s. While these stateless versions ensure that the factories are more flexible, if there is code that attempts the use them the same way as the old stateful factories it does not work.For compatibility, instead of breaking in this scenario, we should make CreationExtras the preferred option over the passed in state. That means we can support CreationExtras while still having the old state as a fallback incase none are provided.