In my application I have 2 fragment and for show these I use NavigationComponent and I want sent some data.
My navigation codes:
<navigation 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:id="@ id/nav_home"
app:startDestination="@id/recipesFragment">
<fragment
android:id="@ id/recipesFragment"
android:name="myapp.ui.fragments.recipe.RecipesFragment"
android:label="@string/recipes"
tools:layout="@layout/fragment_recipes" >
<action
android:id="@ id/action_recipesFragment_to_menuFragment"
app:destination="@id/menuFragment" />
<argument
android:name="isUpdatedData"
app:argType="boolean"
android:defaultValue="false" />
</fragment>
<dialog
android:id="@ id/menuFragment"
android:name="myapp.ui.fragments.recipe.menu.MenuFragment"
android:label="fragment_menu"
tools:layout="@layout/fragment_menu" >
<action
android:id="@ id/action_menuFragment_to_recipesFragment"
app:destination="@id/recipesFragment" />
</dialog>
</navigation>
I set argument with default value, but when use this action into MenuFragment not access to me for set value!
MenuFragment codes:
submitBtn.setOnClickListener {
val action = MenuFragmentDirections.actionMenuFragmentToRecipesFragment(true)
findNavController().navigate(action)
}
When write true for value of actionMenuFragmentToRecipesFragment
show me below error:
Too many arguments for public open fun actionMenuFragmentToRecipesFragment(): MenuFragmentDirections.ActionMenuFragmentToRecipesFragment defined in myapp.ui.fragments.recipe.menu.MenuFragmentDirections
How can I fix it and set data with defaultValue
?
CodePudding user response:
Hi I hope this answer be useful for you
if you would like to use default value below code will help you:
findNavController().navigate(MenuFragmentDirections.actionMenuFragmentToRecipesFragment().setIsUpdatedData(true))
when you set default value for your argument compiler recognize code does not need setter and if you want change it you have to call setter method of argument in your navigation for custom fragment
CodePudding user response:
The action_menuFragment_to_recipesFragment
has not defined that it takes in an argument. You should write it like this:
<dialog
android:id="@ id/menuFragment"
android:label="fragment_menu"
android:name="myapp.ui.fragments.recipe.menu.MenuFragment"
tools:layout="@layout/fragment_menu">
<action
android:id="@ id/action_menuFragment_to_recipesFragment"
app:destination="@id/recipesFragment">
<argument
android:name="isUpdatedData"
app:argType="boolean"
android:defaultValue="false" />
</action>
</dialog>
This way you can both navigate from the MenuFragment
to the RecipesFragment
with or without any arguments:
// With argument
findNavController().navigate(
MenuFragmentDirections.actionMenuFragmentToRecipesFragment(true)
)
// OR without argument (default value = false)
findNavController().navigate(
MenuFragmentDirections.actionMenuFragmentToRecipesFragment()
)