Fixed
Status Update
Comments
il...@google.com <il...@google.com> #2
We should have create(modelClass, extra)
call into create(modelClass)
if you are constructing your AndroidViewModelFactory
using the constructor that takes an Application
instance.
That should ensure that your code will continue to work until you rewrite your ViewModelProvider.Factory
to use CreationExtras
entirely and not need any dependency on AndroidViewModelFactory
at all (since the Application
is provided to every factory). That would be the recommendation longer term :)
ap...@google.com <ap...@google.com> #3
Project: platform/frameworks/support
Branch: androidx-main
commit c644cff0f8261b75be6ea7860bc158a8d030e094
Author: Jeremy Woods <jbwoods@google.com>
Date: Fri Jul 15 15:15:47 2022
Fix AndroidViewModelFactory back compat
Currently, if you create a custom AndroidViewModelFactory while using
Lifecycle 2.4+, and use the stateful constructor, the create() function
without extras will never get called. This means that you are forced to
override the create function that includes extras, which will break
people that are just converting. The create function should continue to
be called just like it was previously.
This change makes sure that when the create function function with
extras is called, if the the stateful constructor was used we properly
fall back to the create function without extras.
RelNote: "Custom implementations of `AndroidViewModelFactory` now
correctly call the `create(modelClass)` function when using the stateful
constructor with `Lifecycle` 2.4+"
Test: added test
Bug: 238011621
Change-Id: I5b315ae80d1498b92e412d9316a90e7fa0e9fb3a
M lifecycle/lifecycle-viewmodel/src/androidTest/java/androidx/lifecycle/AndroidViewModelFactoryTest.kt
M lifecycle/lifecycle-viewmodel/src/main/java/androidx/lifecycle/ViewModelProvider.kt
https://android-review.googlesource.com/2154544
Branch: androidx-main
commit c644cff0f8261b75be6ea7860bc158a8d030e094
Author: Jeremy Woods <jbwoods@google.com>
Date: Fri Jul 15 15:15:47 2022
Fix AndroidViewModelFactory back compat
Currently, if you create a custom AndroidViewModelFactory while using
Lifecycle 2.4+, and use the stateful constructor, the create() function
without extras will never get called. This means that you are forced to
override the create function that includes extras, which will break
people that are just converting. The create function should continue to
be called just like it was previously.
This change makes sure that when the create function function with
extras is called, if the the stateful constructor was used we properly
fall back to the create function without extras.
RelNote: "Custom implementations of `AndroidViewModelFactory` now
correctly call the `create(modelClass)` function when using the stateful
constructor with `Lifecycle` 2.4+"
Test: added test
Bug: 238011621
Change-Id: I5b315ae80d1498b92e412d9316a90e7fa0e9fb3a
M lifecycle/lifecycle-viewmodel/src/androidTest/java/androidx/lifecycle/AndroidViewModelFactoryTest.kt
M lifecycle/lifecycle-viewmodel/src/main/java/androidx/lifecycle/ViewModelProvider.kt
jb...@google.com <jb...@google.com> #4
This has been fixed internally and will be released in the Lifecycle 2.5.1
and 2.6.0-alpha02
versions.
na...@google.com <na...@google.com> #5
This bug was linked in a change in the following release(s):
androidx.lifecycle:lifecycle-viewmodel:2.6.0-alpha02
Description
Component used: androidx.lifecycle.viewmodel
Version used: 2.5.0
Description:
Since lifecycle 2.5.0,
ViewModelProvider
createsViewModel
instance withViewModelFactory.create(modelClass, extra)
method if this method is implemented.In
ViewModelProvider.AndroidViewModelFactory
class case, it provides both ofcreate(modelClass, extra)
andcreate(modelClass)
, but it will always usecreate(modelClass, extra)
only.create(modelClass)
method is not used anymore if class is inherited fromViewModelProvider.AndroidViewModelFactory
,ViewModelProvider.AndroidViewModelFactory.create(modelClass)
create(modelClass)
method only instead ofcreate(modelClass, extra)
forViewModelProvider.AndroidViewModelFactory
class.