Search code examples
androidkotlinandroid-architecture-componentsandroid-navigationandroid-jetpack

Is it possible to send arguments other than string or integer in android's new navigation component


The new navigation component is great! however I would like to send "Long" variables between my fragments.

writing this in the navigation graph file works:

<argument
        android:name="discussionId"
        app:type="string" />

writing this won't compile:

<argument
        android:name="discussionId"
        app:type="long" />

Currently, it seems I'm forced to parse them to and from string formats. It works fine, but it seems bizarre to me that I can't use primitive types such as long or byte or short for such a fundamental architecture. Am I missing something? Is this kind of feature set to be developed in the future?


Solution

  • At the moment you can't use safe args with types apart from integer, string, inferred and reference. An issue has been opened asking for other types.

    But you pass a bundle programmatically when using the navigate() method to navigate to a destination:

    var bundle = bundleOf("key" to amount)
    view.findNavController().navigate(R.id.action_id, bundle)
    

    And you can use the usual getArguments to retrieve the data in the destination fragment:

    val value = arguments.getString("key")