title: Passing data between fragment using navigation UI component
body: <p>I want to pass data between fragments, I have a CharactersFragment and a CharacterDetailFragment, in CharactersFragment I have a recycler view and when I click a item/row, the character details should be sent to ChracterDetailFragment.</p>
  <p>I am using navigation UI component, and I am at the moment passing only character name as string.</p>
  <p>I have a character object, can I pass character object through safeargs, I read that it is not a right approach.</p>
  <p>should I just pass id of the character to the other fragment and retrieve data for that id in the detail fragment?</p>
  <p>what is the best approach to solve this please.</p>
  <p>Here is the code I have written so far</p>
  <p>Thank you very much in advance
  R</p>
  <p>nav_graph</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:id=&quot;@+id/nav_graph&quot;
      app:startDestination=&quot;@id/charactersFragment&quot;&gt;
  
      &lt;fragment
          android:id=&quot;@+id/charactersFragment&quot;
          android:name=&quot;com.example.breakingbad.CharactersFragment&quot;
          android:label=&quot;fragment_characters&quot;
          tools:layout=&quot;@layout/fragment_characters&quot; &gt;
          &lt;action
              android:id=&quot;@+id/action_charactersFragment_to_characterDetailFragment&quot;
              app:destination=&quot;@id/characterDetailFragment&quot; /&gt;
      &lt;/fragment&gt;
      &lt;fragment
          android:id=&quot;@+id/characterDetailFragment&quot;
          android:name=&quot;com.example.breakingbad.CharacterDetailFragment&quot;
          android:label=&quot;fragment_character_detail&quot;
          tools:layout=&quot;@layout/fragment_character_detail&quot; &gt;
          &lt;argument
              android:name=&quot;character&quot;
              app:argType=&quot;string&quot; /&gt;
      &lt;/fragment&gt;
  &lt;/navigation&gt;
  </code></pre>
  <p>CharactersFragment - I have a recyclerview and I am assigning a adapter and passing character name when a row is clicked and navigating to detail screen</p>
  <pre><code>private fun initView() {
          val recyclerView = binding.recyclerView
          recyclerView.layoutManager = LinearLayoutManager(activity)
          val decoration = DividerItemDecoration(activity, DividerItemDecoration.VERTICAL)
          recyclerView.addItemDecoration(decoration)
  
          recyclerAdapter = RecyclerViewAdapter()
          recyclerView.adapter = recyclerAdapter
          recyclerAdapter.onItemClick = { character -&gt;
              val action = CharactersFragmentDirections.actionCharactersFragmentToCharacterDetailFragment(character.name)
              findNavController().navigate(action)
          }
      }
  </code></pre>
  <p>RecyclerViewAdapter</p>
  <pre><code>class RecyclerViewAdapter: RecyclerView.Adapter&lt;RecyclerViewAdapter.MyViewHolder&gt;() {
  
      var items = ArrayList&lt;Character&gt;()
      var onItemClick: ((Character) -&gt; Unit)?  = null
      var itemPosition = 0
  
      fun setUpdatedData(items: ArrayList&lt;Character&gt;) {
          this.items = items
          notifyDataSetChanged()
      }
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
          val view = LayoutInflater.from(parent.context).inflate(R.layout.characters_row, parent, false)
  
          return MyViewHolder(view)
      }
  
      override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
          itemPosition = position
          holder.bind(items.get(position))
      }
  
      override fun getItemCount(): Int {
          return items.size
      }
  
      inner class MyViewHolder(view: View): RecyclerView.ViewHolder(view) {
          val tvName = view.findViewById&lt;TextView&gt;(R.id.tvName)
  
          init {
              view.setOnClickListener {
                  Log.d(&quot;adapter&quot;, &quot;item clicked&quot;)
                  onItemClick?.invoke(items[this.layoutPosition])
              }
          }
  
          fun bind(data: Character) {
              tvName.text = data.name
          }
      }
  }
  </code></pre>
  <p>CharacterDetailFragment</p>
  <pre><code>class CharacterDetailFragment : Fragment() {
  
      val args: CharacterDetailFragmentArgs by navArgs()
  
      private var _binding: FragmentCharacterDetailBinding? = null
      private val binding get() = _binding!!
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          arguments?.let {
          }
      }
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View {
          _binding = FragmentCharacterDetailBinding.inflate(inflater, container, false)
          val view = binding.root
  
          binding.tvNumber.text = args.character.toString()
          return view
      }
  }
  </code></pre>
  <p>Character for ref</p>
  <pre><code>data class Character(
      val name: String,
      val img: String,
      val occupation: ArrayList&lt;String&gt;,
      val status: String,
      val nickname: String,
      val appearance: ArrayList&lt;Int&gt;
  )
  </code></pre>
  <p>Thanks
  R</p>