Search code examples
androidkotlinandroid-jetpack-composegradle-kotlin-dsl

Android jetpack compose navigation between screens error


I got the following error when I try to navigate between screens in jetpack compose, FATAL EXCEPTION: main Process: com.example.notification, PID: 9386 java.lang.IllegalArgumentException: CreationExtras must have a value by SAVED_STATE_REGISTRY_OWNER_KEY

error

My NAVGRAPH

@Composable
fun SetupNavGraph(navController: NavHostController) {
    NavHost(navController = navController, startDestination = Screens.MainScreen.route) {
        composable(
            route = Screens.MainScreen.route
        ) {
            MainScreen(navController)
        }

        composable(
            route = Screens.DetailScreen.route,
        ) {
            DetailsScreen(navController)
        }
    }
}

My screens sealed class

sealed class Screens(val route: String) {
    object MainScreen : Screens(route = "main")
    object DetailScreen : Screens(route = "detail")
}

My ViewModel:

@HiltViewModel
class MainViewModel @Inject constructor(
    private val notificationManager: NotificationManagerCompat,
    private val notificationBuilder: NotificationCompat.Builder
) : ViewModel() {
    @SuppressLint("MissingPermission")
    fun showSimpleNotification() {
        notificationManager.notify(1, notificationBuilder.build())
    }

    @SuppressLint("MissingPermission")
    fun updateNotification() {
        notificationBuilder.setContentText("Updated Content")
        notificationManager.notify(
            1,
            notificationBuilder.setContentText("this is the message from the update").build()
        )

    }

    fun cancelNotification() {
        notificationManager.cancel(1)
    }
}

I try to navigate between tow screens in jetpack compose, i did this many times before but now i got this problem.


Solution

  • I've had a similar problem before. In my case, just in case, I went into the appdan build.gradle and checked the defundancy, and only androidx.hilt:hilt-navigation-compose was connected. That's right. I only connected the Hilt navigation system, but I didn't connect the Compose navigation defense.

    Just add a compost navigation system and we'll be able to solve the problem.

    implementation "androidx.navigation:navigation-compose:2.5.3"
    

    My app only consists of pure Jetpack Compose, so this is the only way to solve the problem, but if you're using Fragment together and you're experiencing problems even if you add the defendancy above, try adding the defendancy below.

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