-
Notifications
You must be signed in to change notification settings - Fork 2k
Hilt - injecting ViewModel via navGraphViewModels issue #2152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the repro case and detailed write up. Just to clarify, you're running into this issue even when not using Hilt, right? The reason this is related to Hilt is that #1935 recommends passing in the defaultViewModelProviderFactory to navGraphViewModels and this causes the issue? |
@Chang-Eric yes, this issue reproduces without Hilt when passing fragment's |
The root cause of the issue is that the We are investigating a solution but sadly I don't have a workaround for now. We might either expose a builder / factory for |
For now, in my sample, what I tried doing is:
And the ugliest part:
Not nice, but state restoration seems to work on my second screen now as well. It's unfortunate that I had not noticed earlier, considering that apparently the views restoring their own state has bamboozled me. 😦 |
Add androidx.hilt:hilt-navigation and androidx.hilt:hilt-navigation-fragment containing APIs for using @hiltviewmodel and Hilt's ViewModelFactory with AndroidX Navigation. The APIs will allow users to retrieve a HiltViewModelFactory whose view model owner and saved state owner is the NavBackStackEntry. Additionally it provide a kotlin extension, `hiltNavGraphViewModels` which mimics `navGraphViewModels`. These artifacts essentially tackle: * google/dagger#2152 * google/dagger#2166 Test: HiltNavGraphViewModelLazyTest Relnote: Provide APIs for using @hiltviewmodel with Navigation. Change-Id: I00e675363f5af3922205a30f4670a4c33877a7b3
Fixed by |
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 - That required updating the Android SDK to 31+, so upgrading to 32 and tools to 32.0.0 (besides a bazel example which has issues with that) - This requires AGP 7.0.0 so dropping support for AGP 4.2.0 - AGP 7.0.0 requires Java 11 so dropping support for Java 8 - Targeting 31+ requires updating androidx.text.core to 1.4.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 - That required updating the Android SDK to 31+, so upgrading to 32 and tools to 32.0.0 (besides a bazel example which has issues with that) - This requires AGP 7.0.0 so dropping support for AGP 4.2.0 - AGP 7.0.0 requires Java 11 so dropping support for Java 8 - Targeting 31+ requires updating androidx.text.core to 1.4.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 - That required updating the Android SDK to 31+, so upgrading to 32 and tools to 32.0.0 (besides a bazel example which has issues with that) - This requires AGP 7.0.0 so dropping support for AGP 4.2.0 - AGP 7.0.0 requires Java 11 so dropping support for Java 8 - Targeting 31+ requires updating androidx.text.core to 1.4.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 - That required updating the Android SDK to 31+, so upgrading to 32 and tools to 32.0.0 (besides a bazel example which has issues with that) - This requires AGP 7.0.0 so dropping support for AGP 4.2.0 - AGP 7.0.0 requires Java 11 so dropping support for Java 8 - Targeting 31+ requires updating androidx.text.core to 1.4.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 449844449
- Fixes issues with keyed ViewModels. - Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed. - Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0 - That required updating the Android SDK to 31+, so upgrading to 32 and tools to 32.0.0 (besides a bazel example which has issues with that) - This requires AGP 7.0.0 so dropping support for AGP 4.2.0 - AGP 7.0.0 requires Java 11 so dropping support for Java 8 - Targeting 31+ requires updating androidx.text.core to 1.4.0 Fixes #3232. Issue #2152. RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK. PiperOrigin-RevId: 461696901
Hello,
I had an issue with restoring state of injected viewmodel via navGraphViewModels.
I saw the recommended way for injectig viewmodels with the Jetpack navigation library (#1935), 2, however after process death
ViewModel
is not restored correctly on the second screen of navGraph.I created demo project which demostrates the issue. We have a nested navigation graph
R.id.nested
which consists of 2 fragments. Those 2 fragments sharesMainViewModel
andMainViewModel2
which are completely similar and containsLiveData<String>
.The only difference is that first ViewModel initialized with fragment's
defaultViewModelProviderFactory
as recommended with Hilt, while second is initialized without passing custom factory, so uses backStackEntry'sdefaultViewModelProviderFactory
(will not work when Hilt is used, but to demosntrate the problem).First fragment in nested graph sets message to both viewmodels and then opens Second fragment.
With
Don't keep activities
enabled, putting app to background and back, triggers app process death and restore.After process restore,
MainViewModel
doesn't have the value from First fragment and shows the default one, whileMainViewModel2
is restored correctly and displays proper message.On SecondFragment both viewmodels have correct values.
Don't keep activities
to trigger process death and come back.On SecondFragment first viewmodel doesn't have restored value, second viewmodel has as expected.
On SecondFragment both viewmodels have correct values.
Don't keep activities
to trigger process death and come back.On SecondFragment both viewmodels contains correct value, even if you modify value on FirstFragment.
Don't keep activities
and open SecondFragment.On SecondFragment both viewmodels have correct values.
Don't keep activities
to trigger process death and come back.On SecondFragment first viewmodel doesn't have restored value, second viewmodel has as expected.
The text was updated successfully, but these errors were encountered: