I've converted my app to use NavigationUI. But setupWithNavController() replaces my setNavigationItemSelectedListener().
In my listener I was passing a (type safe) argument to the fragment.
I've converted my app to use NavigationUI. But setupWithNavController() replaces my setNavigationItemSelectedListener().
In my listener I was passing a (type safe) argument to the fragment.
If I understand your question correct what you are looking for is:
Navigation parameters documentation
Briefly:
In your navigation graph, on the fragment you want the value sent to, you will add an argument.
<fragment android:id="@+id/myFragment" >
<argument
android:name="amount"
app:argType="integer"
android:defaultValue="0" />
</fragment>
When you then reference that fragment as an action, the library will generate a Directories class, which will look something like this:
val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
v.findNavController().navigate(action)
The names are generated based upon what you named the Fragment and actions. Also note that it is possible to send whole objects if they are Parcelable as well.
If the Fragment in question is the startDestination of your graph I am not sure if you can do it right away. A work around might be to load the data in the Fragment? Or maybe into a shared ViewModel from the Activity?
Good luck!
If anyone is still looking for an answer to this question. I've found a usefull workaround with the NavigationUI Global actions (working with alpha-09)
You can provide a top level action to your navigation graph:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_nav"
app:startDestination="@id/mainFragment">
...
<action android:id="@+id/action_global_mainFragment"
app:destination="@id/mainFragment"/>
</navigation>
And use it with your navigation controller to navigate with the correct bundle when a user click on a menu item. Be carefull to remove .setupWithNavController(navController) call from your NavigationView, or it will override your behavior.