0

In an activity two fragments are passing some string when switching.

Lobby Fragment

        button.setOnClickListener {
            LobbyFragmentDirections.actionLobbyFToGameF(myTitle)
            findNavController(requireActivity(), R.id.fragment_container_view).navigate(R.id.gameFragment)
        }

Game Fragment

private val args: GameFragmentArgs by navArgs()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_game, container, false)
    UseArgument(args.argumentTitle)
}

Navigation map : Game Fragment Attributes

map

Problem

When getting the arguments in the Game Fragment onViewCreated, it throws an java.lang.IllegalArgumentException: Required argument "argumentTitle" is missing

Foxhunt
  • 764
  • 1
  • 9
  • 23

1 Answers1

0
button.setOnClickListener {
    val dir = LobbyFragmentDirections.actionLobbyFToGameF(myTitle)
    findNavController().navigate(dir)
}
IR42
  • 8,587
  • 2
  • 23
  • 34
  • Works fine. Thanks ! When not passing arguments does that make any difference to use direction like this instead of passing the destination like my first try ? – Foxhunt Oct 30 '21 at 21:58
  • 1
    @Foxhunt There are two differences, the first the argument won't be passed to the destination unless you use the traditional Bundle object; the second is that you're not using Safe Args of the navigation components; and besides it allows to pass arguments, it also prevents runtime errors, because it checks that there is an argument, and that it's of the same type as you intend in the navGraph. – Zain Oct 30 '21 at 22:33
  • Ok ^^ I found more details here https://stackoverflow.com/questions/57608291/difference-between-actions-and-destination-in-navigation-component – Foxhunt Oct 30 '21 at 22:50