How to automatically load contents of recycler when using nav componets.. And not Activity intentPutExtra

39 Views Asked by At

I am trying to load the contents of recyclerView on a new screen I navigate to using navigation components.. I think with activity it was intentPutExtra, or something like this.

I'd appreciate if you could write a sample code, but I don't mind any pointers, thanks in advance. I'm new, and I don't really to the informartion to provide, so please if you need anything I am more than happy to provide, thanks in advance.

Here's the code from the recyclerView Adapter, that navigate into the fragment:


    // binds the list items to a view
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {


        ...


        // navigates to stories content destination
        holder.storiesCardView.setOnClickListener {
            val action = StoriesFragmentDirections.actionNavigationStoriesToStoriesContentFragment()
            holder.itemView.findNavController().navigate(action)
        }

    }

So I'm using safeArgs(ver 2.3.1) FragmentDirections to navigate.

Please if you decide to show a sample code, I intend to use predefined text in string.xml file.

I'd love to add more info but I want this to be short as possible. So please if you want any info let me know.. Thanks again..

1

There are 1 best solutions below

2
BabaVarma On

At ur navgraph

  <fragment
                    android:id="@+id/destination_fragment_id"
                    android:name="app.test.DestinationFragment"
                    android:label="@string/destination_fragment_label"
                    tools:layout="@layout/destination_fragment_layout">
        
                <argument
                        android:name="variableName"
                        app:argType="app.test.data.model.CustomModel" 
 ../>
    </fragment>
    

at navigating place

val direction = StoriesFragmentDirections.actionNavigationStoriesToStoriesContentFragment(customModel)
    findNavController().navigate(direction)

Note:CustomModel should be Parcelable or Serializable.

at retriving in fragment like below

 val variableName by lazy {
       fromBundle(arguments!!).variableName
   }