Automatic Argument Storage in SavedStateHandle by Navigation Component

99 views Asked by At

I'm using the Navigation component in my Jetpack Compose project and have a question about how argument values are accessed in SavedStateHandle of ViewModels.

When I define a route with arguments in my NavGraph and navigate to it, I can directly retrieve those argument values from the SavedStateHandle in the corresponding ViewModel, without explicitly setting them there myself.

Does the Navigation component internally store these argument values in the SavedStateHandle under their corresponding keys automatically? If so, could you please provide an explanation , insights or clarifications on this mechanism?

Here's a code snippet to illustrate setup(Reference : https://developersbreach.com/savedstatehandle-viewmodel-android/) :

// NavGraph composable
composable(
    route = "detail_route/movieId",
    arguments = listOf(
        navArgument("movieId") {
            type = NavType.IntType
        }
    )
) {
    val viewModel = hiltViewModel<DetailViewModel>()
    DetailScreen(
        viewModel = viewModel
    )
}

// DetailViewModel
class DetailViewModel(
    savedStateHandle: SavedStateHandle
) : ViewModel() {

    private val movieId: Int? = savedStateHandle["movieId"]

    fun getMovieInformation() {
        val movieData: Movie = repository.getSelectedMovieData(movieId)
    }
}
2

There are 2 answers

2
usr153.K On

Like Fragment stacks and Bundle arguments, NavController has features to manage navigation back stack NavBackStackEntry and its arguments.

When a movie list screen goes to a detail screen, NavHost(NavController) searches for a NavDestination and a movie detail back stack is pushed into NavHost with arguments from movie list NavArgumentType.

NavController in Compose functionality is so similiar with FragmentManager in fragment.

enter image description here

0
waseem akram On

Accourding to me Navigation component automatically stores argument values in the SavedStateHandle of the associated ViewModel under their

Passing Arguments, 
ViewModel Creation, 

When navigating from one destination to another using the Navigation component, you can pass arguments along with the navigation action. These arguments are typically defined in the navigation graph XML file.

If you've associated a ViewModel with a particular destination in your navigation graph, the Navigation component automatically creates and associates a ViewModel instance with that destination when needed. This is often done via the by viewModels() Kotlin property delegate or ViewModelProvider in Java.

class viewModel(private val savedStateHandle: SavedStateHandle) : ViewModel() {
    // Accessing argument values
    val userId: String? = savedStateHandle.get<String>("userId")

    // Your logic logic...
}

in this if you have an argument named "userId" associated with the destination in your navigation graph, the Navigation component will automatically inject its value into the SavedStateHandle with the key "userId" when creating the ViewModel.