koin
koin copied to clipboard
SavedStateHandle doesn't contain navigation arguments
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.
I have also tried using viewModel { (handle: SavedStateHandle) -> DetailsViewModel(handle, get()) }
Hello! The same thing is happening to me, I think the case is related. According to the google changelog it should pick up the arguments via SavedStateHandle
https://developer.android.com/jetpack/androidx/releases/navigation#2.4.0
But this does not happen, here is my example.
Module:
viewModel { (handle: SavedStateHandle) ->TestViewModel(savedStateHandle = handle) }
Fragment:
val viewModel: TestViewModelby stateViewModel()
ViewModel:
class TestViewModelby (savedStateHandle: SavedStateHandle)
Looking at the debug tab it seems that SavedStateHandle does not pick up the arguments from viewmodel
But if I look at the test arguments in the Fragment, it does have arguments.
If I try this using test by viewmodels() //androidx.fragment.app.viewModels
It seems that if I get the arguments of the args from SavedStateHandle in the viewmodel, here is an example:
I hope this helps to fix this :)
Thanks!
Let's hope its available soon 😃 https://github.com/InsertKoinIO/koin/pull/1286
Thank you very much! :D
Looks like a good candidate for the patch. Any plans to release 3.2.1 with it?
Please review the pr which conforms with 3.2 https://github.com/InsertKoinIO/koin-compose/pull/3
Still stuck with this one.
Me too.
Workaround
private val EmptyBundle = Bundle()
fun Fragment.argumentsToBundleDefinition(): BundleDefinition = {
if (arguments != null)
arguments!!
else
EmptyBundle
}
inline fun <reified VM : ViewModel> Fragment.injectViewModel(
qualifier: Qualifier? = null,
noinline state: BundleDefinition = argumentsToBundleDefinition(),
noinline owner: ViewModelStoreOwnerProducer = { this },
noinline parameters: ParametersDefinition? = null,
): Lazy<VM> = stateViewModel(qualifier, state, owner, parameters)
Should be fixed in koin-androidx-compose
3.2.1
PR https://github.com/InsertKoinIO/koin-compose/pull/3 has been merged
Thanks @arnaudgiuliani from 3.2.1
the SavedStateHandle
started storing navigation arguments using getViewModel
composable