Home > OS >  Android - Pass Argument with safe args
Android - Pass Argument with safe args

Time:10-31

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

CodePudding user response:

button.setOnClickListener {
    val dir = LobbyFragmentDirections.actionLobbyFToGameF(myTitle)
    findNavController().navigate(dir)
}
  • Related