45

I'm trying to implement navigation in my app which is built with Jetpack Compose, but when I try to navigate from a screen to another I get:

java.lang.IllegalArgumentException: CreationExtras must have a value by SAVED_STATE_REGISTRY_OWNER_KEY

I'm using:

implementation "androidx.hilt:hilt-navigation-compose:1.0.0"

And here is the code:

if(viewModel.isAuthenticated) {
    navController.navigate(Screen.Profile.route)
}
Joan P.
  • 2,368
  • 6
  • 30
  • 63
  • 2
    For me this issue is resolved by updating to version `1.1.0-alpha01` of the hilt navigation compose library – MEX Feb 03 '23 at 10:49
  • @MEX It also works that way, I tested it. If you want, you can post it as an answer. – Joan P. Feb 04 '23 at 08:50

13 Answers13

51

@Dragan.T 's answer is correct.

Adding

implementation "androidx.navigation:navigation-compose:2.5.3

Solved my problem. As for why we need to add, I am not so sure.

Edit: Be sure to replace 2.5.3 with latest version.

Boken
  • 4,825
  • 10
  • 32
  • 42
Zeeshan Ali
  • 623
  • 1
  • 5
  • 10
  • I cannot see any reason why we need another dependency as long as we are using one that provides everything that is necessary. – Joan P. Aug 26 '22 at 06:58
  • You are using hilt navigation not compose navigation so maybe hilt navigation provides stuff only related to hilt injection not for compose viewmodel creation. I am also not sure about this but the solution did work – Zeeshan Ali Aug 26 '22 at 13:31
  • It already provides stuff for navigation, so it's not about that. – Joan P. Aug 27 '22 at 09:09
  • 1
    It worked for me too, 2.4.2 had problem but it was removed after I changed to 2.5.1. – Isaac Lee Oct 09 '22 at 01:41
21

If you use Compose with Fragments, then you may not have the Fragments dependency where viewModels() is defined.

Adding:

implementation "androidx.fragment:fragment-ktx:1.5.2"

to my build.grade script fixed it for me (previously this was a transitive dependency).

Patrick
  • 855
  • 8
  • 12
  • Agree, my project was using pure Hilt+Fragments+viewModels() (No navigation components yet) and I have got a runtime error "java.lang.IllegalArgumentException: CreationExtras must have a value by SAVED_STATE_REGISTRY_OWNER_KEY" in the line where Hilt was injecting the viewModel by viewModels()/ Simply updating the androidx.fragment:fragment-ktx library from 1.4.1 to 1.5.2 fixed the error. – Alex Burov Nov 09 '22 at 19:33
  • This worked for me. I'm not using Jetpack Compose, but I am using `Hilt+Fragments+viewModels()` like the above person. I needed this. Wish Hilt document mentioned this. Thank you. – Saehun Sean Oh Feb 22 '23 at 19:19
20

Hilt release notes for 2.43 flags a dependency incompatibility:

As part ViewModel bug fixes, dependencies were updated as below. androidx.navigation users will need to update to 2.5.0 to interoperate. These libraries require building with SDK 31. To build with SDK 31, AGP users will need to use AGP 7.0+. This will also require using JDK11.

  • androidx.activity and androidx.fragment to 1.5.0
  • androidx.lifecycle to 2.5.0
  • androidx.savedstate to 1.2.0
Sofi Software LLC
  • 3,879
  • 1
  • 36
  • 34
17

Insert implementation "androidx.navigation:navigation-compose:2.5.1" into your gradle file. If you already have it, be sure it's updated with the latest (2.5.1) version.

Dragan.T
  • 317
  • 1
  • 5
  • 1
    Thanks for taking the time to answer the question. But why would I add another dependency since I already have one? Hilt already provides all the necessary methods for navigation, right? – Joan P. Aug 17 '22 at 10:43
  • 1
    Hilt-navigation-compose library just provide methods and annotations for creating necessary scopes and init navigation graph with dependency injection, but you should add "androidx.navigation:navigation-compose:2.5.1" for working with compose nav. – Dragan.T Aug 19 '22 at 12:32
  • 1
    `androidx.hilt:hilt-navigation-compose:1.0.0` already contains a navigate method, I have no error in code. It just simply complains when I need to navigate from one screen to another. – Joan P. Aug 20 '22 at 10:07
7

This issue is resolved by updating to version 1.1.0-alpha01 of the hilt navigation compose library.

MEX
  • 2,664
  • 5
  • 22
  • 28
4

Just adding this to your app/build.gradle fixed this issue:

implementation 'androidx.navigation:navigation-compose:2.6.0'

Example:

dependencies {

    // ...
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
    implementation 'androidx.navigation:navigation-compose:2.6.0'
}
ahmnouira
  • 1,607
  • 13
  • 8
2

In my case I had to upgrade two dependencies: Hilt and androidx.navigation. The curious issue is that after upgrading it, the error is gone, but if I downgrade again to previous versions, the error doesn't appear again. These are the dependencies I changed:

    navigation_version = "2.5.3" // was 2.5.1
    dagger_hilt_version = "2.44.2" // was 2.43.2

    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$navigation_version"
    androidTestImplementation "androidx.navigation:navigation-testing:$navigation_version"

    implementation "com.google.dagger:hilt-android:$dagger_hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$dagger_hilt_version"
    testImplementation "com.google.dagger:hilt-android-testing:$dagger_hilt_version"
    kaptTest "com.google.dagger:hilt-android-compiler:$dagger_hilt_version"
    androidTestImplementation "com.google.dagger:hilt-android-testing:$dagger_hilt_version"
    kaptAndroidTest "com.google.dagger:hilt-android-compiler:$dagger_hilt_version"
voghDev
  • 5,641
  • 2
  • 37
  • 41
2

In my case I had to update the

implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'

to

implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
Edwin Paz ss. -
  • 177
  • 2
  • 5
0

You can to continue use this library:

androidx.hilt:hilt-navigation-compose:1.0.0

Only you are sure that ViewModel is initialized in a Composable that is root in your activity. For example:

    setContent {
        AndroidLearningTheme {
            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colors.background
            ) {
                HeroesScreen()
            }
        }
    }

Your Composable

@Composable
fun HeroesScreen(
    viewModel: HeroesViewModel = hiltViewModel()
)
  • Oh, thank you Carlos for taking the time to answer this unsolved issue. I didn't try it yet. So, let's it works, how about the other composables? – Joan P. Oct 06 '22 at 05:06
  • I tried to create the ViewModel only inside the activity and pass them as parameter, but it doesn't work either. I get the same error :( – Joan P. Oct 06 '22 at 05:24
  • Sorry, don't worries, maybe I need more code for to replicate the error, can you share the repository project with error? @JoanP. – Carlos Buruel Ortiz Oct 06 '22 at 17:06
0

I faced this error due to ViewModelProviders and after spending a long time and changing to ViewModelProvider than it get fixed

ViewModelProviders is deprecated

Old:

ViewModelProviders.of(context, context.defaultViewModelProviderFactory)
    .get(XYZViewModel::class.java)

New one fixed:

ViewModelProvider(
    context,
    context.defaultViewModelProviderFactory
)[XYZViewModel::class.java]
Attif
  • 1,158
  • 13
  • 17
0

It seems that updating the versions of the following AndroidX libraries might resolve the issue:

  • androidx.activity
  • androidx.fragment
  • androidx.lifecycle
  • androidx.savedstate

Alternatively, you could use the following versions:

dependencies {
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
    implementation 'androidx.savedstate:savedstate:1.2.1'
    implementation 'androidx.lifecycle:lifecycle-common:2.6.1'
    implementation 'androidx.activity:activity-ktx:1.7.0'
    implementation 'androidx.fragment:fragment-ktx:1.5.6'
}

These versions have been tested and found to work well together for me.

GaneshHulyal
  • 86
  • 1
  • 4
0

Updating androidx.navigation:navigation-compose library to the current stable version 2.5.3 solved my issue.

Jan Kotas
  • 71
  • 1
  • 3
0

For me, adding implementation("androidx.lifecycle.lifecycle-viewmodel-compose:2.6.1") fixed it.

Edward Sills
  • 103
  • 1
  • 3