how to send navigator extras with Directions in findNavController().navigate()

Viewed 2120

I what to set a transition animation for my view

search about it and understand must send navigator extras in navigate metod

its work for normal navigation but when what send argument with navigation show me this error

None of the following functions can be called with the arguments supplied: private open fun navigate(p0: NavDestination, p1: Bundle?, p2: NavOptions?, p3: Navigator.Extras?): Unit defined in androidx.navigation.NavController public open fun navigate(p0: Int, p1: Bundle?, p2: NavOptions?, p3: Navigator.Extras?): Unit defined in androidx.navigation.NavController

it's about first argument error

and this is my code

val extras = FragmentNavigatorExtras(image_cover to "image_cover")
    findNavController().navigate(
        MoviesFragmentDirections.actionMoviesFragmentToMovieItemFragment(
            moviesEntity.title,
            moviesEntity.image,
            moviesEntity.text,
            moviesEntity.video
        ),
        null,
        null,
        extras
    )
1 Answers

You can use the following "navigate" function variation to send the extras added to the NavDirections:

 /**
 * Navigate via the given {@link NavDirections}
 *
 * @param directions directions that describe this navigation operation
 * @param navigatorExtras extras to pass to the {@link Navigator}
 */
public void navigate(@NonNull NavDirections directions,
        @NonNull Navigator.Extras navigatorExtras) {
    navigate(directions.getActionId(), directions.getArguments(), null, navigatorExtras);
}

Modify your code like that to achieve that:

val extras = FragmentNavigatorExtras(image_cover to "image_cover")
findNavController().navigate(
    MoviesFragmentDirections.actionMoviesFragmentToMovieItemFragment(
        moviesEntity.title,
        moviesEntity.image,
        moviesEntity.text,
        moviesEntity.video
    ),
    extras
)
Related