Skip to content
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

Hilt - injecting ViewModel via navGraphViewModels issue #2152

Closed
silentnuke opened this issue Oct 19, 2020 · 5 comments
Closed

Hilt - injecting ViewModel via navGraphViewModels issue #2152

silentnuke opened this issue Oct 19, 2020 · 5 comments

Comments

@silentnuke
Copy link

silentnuke commented Oct 19, 2020

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 shares MainViewModel and MainViewModel2 which are completely similar and contains LiveData<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's defaultViewModelProviderFactory (will not work when Hilt is used, but to demosntrate the problem).

    private val viewModel by navGraphViewModels<MainViewModel>(R.id.nested) {
        defaultViewModelProviderFactory
    }
    
    private val viewModel2 by navGraphViewModels<MainViewModel2>(R.id.nested)

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, while MainViewModel2 is restored correctly and displays proper message.

  1. Enter message on FirstFragment and open SecondFragment.
    On SecondFragment both viewmodels have correct values.
  2. Background app with 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.
  3. Press back to return on FirstFragment and open SecondFragment again
    On SecondFragment both viewmodels have correct values.
  4. Background app with 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.
  5. Press back to return on FirstFragment, Background app with Don't keep activities and open SecondFragment.
    On SecondFragment both viewmodels have correct values.
  6. Background app with 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.

demo

@silentnuke silentnuke changed the title Hilt - injecting ViewModel via navGraphViewModels Hilt - injecting ViewModel via navGraphViewModels issue Oct 19, 2020
@Chang-Eric
Copy link
Member

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?

@silentnuke
Copy link
Author

@Chang-Eric yes, this issue reproduces without Hilt when passing fragment's defaultViewModelProviderFactory to navGraphViewModels.
The reason this is related to Hilt is that with Hilt there is no way to use navGraphViewModels without passing fragment's defaultViewModelProviderFactory and this is recommened way in the docs. Without Hilt backStackEntry's defaultViewModelProviderFactory is used by default which doesn't have that issue.

@danysantiago
Copy link
Member

The root cause of the issue is that the defaultViewModelProviderFactory from an activity or fragment uses the activity itself as the SavedStateRegistryOwner and that is different from the ViewModelStoreOwner used by the NavBackStack. It seems these two should be the same, which is exactly what is going on with the HiltViewModelFactory too, it uses the activity or fragment as SavedStateRegistryOwner which is different from the ViewModelStoreOwner that is used by the ViewModelProvider by theNavBackStack.

We are investigating a solution but sadly I don't have a workaround for now. We might either expose a builder / factory for HiltViewModelFactory (which is odd) or we might work with AndroidX itself to make it so that these two owners are in sync.

@Zhuinden
Copy link

Zhuinden commented Dec 1, 2020

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. 😦

copybara-service bot pushed a commit to androidx/androidx that referenced this issue Jan 13, 2021
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
@Zhuinden
Copy link

Fixed by hiltNavGraphViewModels in hilt-navigation-fragment

copybara-service bot pushed a commit that referenced this issue Jul 11, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 11, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 11, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 11, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 12, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 12, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 12, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 12, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 12, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 14, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 14, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 14, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 14, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 18, 2022
- 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
copybara-service bot pushed a commit that referenced this issue Jul 18, 2022
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants