I'm learning Navigation Component and I'm just wondering the difference between the two
view.findNavController().navigate(R.id.action_gameFragment_to_gameWonFragment)
and
view.findNavController().navigate(GameWonFragmentDirections.actionGameWonFragmentToGameFragment())
is using Safe args generates nav direction classes
more efficient???
CodePudding user response:
It's not bad to read the Documents
The recommended way to navigate between destinations is to use the Safe Args Gradle plugin. This plugin generates simple object and builder classes that enable type-safe navigation and argument passing between destinations.
SafeArgs is a Gradle plugin that allows you to enter information into the navigation graph about the arguments you want to pass. It then generates code for you that creates a Bundle for these arguments on your behalf and pulls them from that Bundle so that you can use them
So when you want to pass data between fragments it's recommended to use safeArgs (less code for us to maintain, type-safety for your arguments), but still you can use raw Bundles to do that.
In conclusion, if you want to navigate from one fragment to another, you can use any of those methods you mentioned above with no difference in the result. If you also want to pass data between them, SafeArgs just generates the required code for navigation to make data transfer more manageable for you