Android Navigation component - can't use Extras and Safe Args
Asked Answered
Z

0

10

I'm using the Android Navigation component with SafeArgs, and I want to add a shared element transition to an action. According to the documentation, I should be able to simply add an Extras object as a parameter to navigate(), so I try that:

FragmentNavigator.Extras extras = new FragmentNavigator.Extras.Builder()
                .addSharedElement(view, "group_transition")
                .build();
NavDirections action = HomeFragmentDirections.actionHomeFragmentToViewFragment(title);
Navigation.findNavController(view).navigate(action, extras);

but Android Studio gives an error, and says it can't resolve the method. I try to use

.navigate(int resId, Bundle args, NavOptions navOptions, Navigator.Extras navigatorExtras)

which narrows the problem down to the Navigator.Extras. Android Studios puts an error under that argument, telling me:

Required Type: androidx.navigation.Navigator.Extras,
Provided Type: androidx.navigation.fragment.FragmentNavigator.Extras"

Which I can't figure out. Navigator.Extras is an interface, which FragmentNavigator.Extras implements. What can I do about this, other than giving up on the transition I want?

Zaratite answered 4/3, 2022 at 21:14 Comment(3)
I think this is a bug in Android Studio. The project still builds and app runs fine with this error.Pittsburgh
After two months, still no one has a solution?Cleanly
This Android Studio bug is also present in Android Studio Chipmunk without SafeArgs plugin. As @Amit Jayant says, the project still builds OK and transitions behave as expected.Spriggs

© 2022 - 2024 — McMap. All rights reserved.