0

I have a fragment A which contains this code :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    .......

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/children_container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/layout_navigation" />

    .......

</RelativeLayout>

that start my fragment B. I would like to know how I can pass some argument from fragmentA and get in my fragmentB.

By reading this link but its talked about Activity A from Activity B.

I tried to remove this line from my FragmentContainerView

app:navGraph="@navigation/layout_navigation"

And in my fragmentA I added this following line :

val bundle = Bundle().apply {
    putString(KEY, "value")
}

NavHostFragment.create(R.navigation.layout_navigation, bundle)

In my fragment B, my arguments is null

Do you have any idea?

LikseN
  • 139
  • 1
  • 1
  • 7
  • `NavHostFragment.create(R.navigation.layout_navigation, bundle)` I don't understand this line at all. You should be using `Navigation.findNavController(this).navigate(R.id.some_destination, bundle)`. – EpicPandaForce Apr 26 '20 at 03:32
  • 1
    for some reason I had to do this .. : `val fragment = childFragmentManager.findFragmentById(R.id.children_container) ?: return` `val navController = NavHostFragment.findNavController(fragment)` Then it works ... – LikseN Apr 27 '20 at 08:32

1 Answers1

0

Open your navigation graph xml file and select a fragment that will receive some data. On the right side of your screen are the fragment attributes.

enter image description here

Select "Arguments" option and click +. Then enter arguments name, data type etc.

enter image description here

When you enter data in the dialog, click "Add".

Now open your "fragmentA" and init navController

val navController = Navigation.findNavController(view)

And navigate to your "fragmentB" with data in Bundle

view.findViewById<Button>(R.id.openB).setOnClickListener { // <==== YOUR CLICK LISTENER THAT NAVIGATE TO FRAGMENT B
                navController.navigate(
                    R.id.action_fragmentA_to_fragmentB, // <==== YOUR ACTION ID (you can find it in your navigation graph XML file)
                    Bundle().apply {
                        putString("KEY", "My data") // <==== YOUR KEY AND DATA(data type should be similar to what you specified when create arguments in navigation graph XML file)
                    }
                )
            }

Now go to "fragmentB" .java or .kt file and receive data by key

val data = arguments?.getString("KEY")

Hope my answer helps you!!

You can also read this article => pass data between fragments in nav component

lincollincol
  • 595
  • 2
  • 10
  • 16
  • I can't use : findNavController in my fragment A, when I used it I have this following error : does not have a NavController set. In fact, my fragment A should be the startDestination, but because of this following error that I didnt understand, I create a new Fragment B to be the startDestination. Thats why I would like to pass some arguments from fragmentA to fragmentB – LikseN Apr 23 '20 at 09:42
  • @LikseN try to replace in fragmet A layout to Read more here => https://stackoverflow.com/questions/50502269/illegalstateexception-link-does-not-have-a-navcontroller-set – lincollincol Apr 23 '20 at 09:48
  • nothing changed, still get the same error @lincollincol – LikseN Apr 23 '20 at 12:25