Home > database >  Why I can´t pass arguments with global action in navigation component?
Why I can´t pass arguments with global action in navigation component?

Time:09-30

I´m trying to pass an argument with an global action from one destination to another. I saw this method on the internet but it don´t work for any reasons and I can´t find a solution for it. I would be thankful for any help. How I´m trying to pass the argument:

epoxyController = EventListEpoxyController { eventClickedID ->
        val navDirections = NavGraphDirections.actionGlobalEventHolder(eventID = eventClickedID)
        findNavController().navigate(navDirections)

I added an argument to the eventholder destination in my navgraph and also added an global action. But for some reasons I can´t find a value for it the word is red and the error is like

Named arguments are not allowed for non-Kotlin functions Cannot find a parameter with this name: eventID < Thats how my navgraph looks like :

<fragment
    android:id="@ id/eventHolder"
    android:name="de.n.newtest.ui.Fragments.Details.EventHolderKT"
    android:label="fragment_event_holder">
    <argument
        android:name="eventid"
        android:defaultValue="-1"
        app:argType="integer" />
<action
    android:id="@ id/action_global_eventHolder"
    app:destination="@id/eventHolder"
    app:enterAnim="@anim/holderslideinright"
    app:exitAnim="@anim/holderslideoutleft"
    app:popEnterAnim="@anim/holderslideinleft"
    app:popExitAnim="@anim/holderslideoutright">

What did I miss?

CodePudding user response:

val navDirections = NavGraphDirections.actionGlobalEventHolder(eventID = eventClickedID)

The eventID is created by the IDE to illustrate that this parameter is the callback parameter that points to eventClickedID, and it's not supposed to write it yourself as the error implies:

Named arguments are not allowed for non-Kotlin functions Cannot find a parameter with this name: eventID < Thats how my navgraph looks like :

So, you just need to remove it:

epoxyController = EventListEpoxyController { eventClickedID ->
        val navDirections = NavGraphDirections.actionGlobalEventHolder(eventClickedID)
        findNavController().navigate(navDirections)
}

CodePudding user response:

You're using android:name="eventid". That means that the named parameter for actionGlobalEventHolder is eventid, not eventID.

You'll want to either change your android:name to eventID or update your code to use eventid.

Of course, using a named argument is never a requirement, so you could leave it out entirely.

  • Related