3

I'm trying to inject SavedStateHandle. I am using Koin, Jetpack Compose and Navigation. I've managed to make it work, but I think it's not the right instance (I don't get my values).

This is what I do so far:

Setting the argument:

private fun NavGraphBuilder.addDetails(
    navController: NavHostController,
    root: Screen
) {
    composable(route = LeafScreen.Details.createRoute(root),
        arguments = listOf(
            navArgument("showId") { type = NavType.LongType }
        )) {
        DetailsScreen(navigateUp = navController::navigateUp)
    }
}

Defining my ViewModel:

internal class DetailsViewModel(
    val handle: SavedStateHandle,
    private val getMovieDetailsUseCase: GetMovieDetailsUseCase
) : ViewModel() 

Defining my composable:

@Composable
fun DetailsScreen(
    navigateUp: () -> Unit
) {
    DetailsScreen(
        viewModel = getStateViewModel(),
        navigateUp = navigateUp,
    )
}

And my modules:

val uiDetailsModules = module {
    viewModelOf(::DetailsViewModel)
}

It runs but showId from val showId = handle.get<Long>("showId") is null.

grrigore
  • 1,050
  • 1
  • 21
  • 39
  • `getStateViewModel` returns module scoped view model, use `getViewModel` instead - this view model is route scoped and so `SavedStateHandle` should work – Phil Dukhov May 24 '22 at 10:37
  • That won't work. Now I get an error: `Caused by: org.koin.core.error.NoBeanDefFoundException: |- No definition found for class:'androidx.lifecycle.SavedStateHandle'. Check your definitions!` – grrigore May 24 '22 at 10:59
  • not sure what's `viewModelOf`, it should be `viewModel { (handle: SavedStateHandle) -> DetailsViewModel(handle, get()) }` – Phil Dukhov May 24 '22 at 11:03
  • Now I get a new error `org.koin.core.error.NoParameterFoundException: Can't get injected parameter #0 from DefinitionParameters[] for type 'androidx.lifecycle.SavedStateHandle'` – grrigore May 24 '22 at 11:06
  • https://insert-koin.io/docs/reference/koin-android/viewmodel/#state-handle-injection – Phil Dukhov May 24 '22 at 13:55
  • Yes, it doesn't work... – grrigore May 25 '22 at 08:00
  • Please edit your question with `createRoute` used in `LeafScreen.Details.createRoute(root)` and with how you call `navigate` with your long argument – Phil Dukhov May 28 '22 at 09:18
  • @PhilDukhov sorry for the delayed response. [Here](https://pastebin.com/xQEsNx4z) is my code – grrigore Jul 13 '22 at 08:21
  • I have the same problem, but what I found is that value is actually stored in SavedStateHandle, the only problem is it's always String, no matter what type was set in `navArgument("showId") { type = NavType.LongType }` For now I am using workaround like `val showId = handle.get("showId").toLong()`. If you have the same situation, then it can be a bug with compose navigation. – Ruslan Dec 29 '22 at 17:40
  • This should be solved in newer versions of the library. Regarding your issue, I don't really know how it works. As far as I remember, in my case it was ok. – grrigore Jan 03 '23 at 12:31

0 Answers0