2

If I am using sage-args an directions and they do not have possibility to pass arguments and then I do

navController.navigate(R.id.action_to_authenticationFlow, bundle)

I am not getting this arguments in Fragment

 val uriString = arguments?.getString("uri")

Is it ok and I need to config safe args or something is done wrong with bundle passing?

UPDATE

I've changed to suggested solution but I think there is problem that I have nested login.xml graph and trying to pass this arguments to fragment in this nested graph.

login.xml

<?xml version="1.0" encoding="utf-8"?>
<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/loginFlow"
    app:startDestination="@id/authenticationFragment">

    <fragment
        android:id="@+id/authenticationFragment"
        android:name="com.softne.crm.ui.authentication.AuthenticationFragment"
        android:label="fragment_authentication"
        tools:layout="@layout/fragment_authentication">
        <argument
            android:name="uri"
            app:argType="string"
            app:nullable="true" />
    </fragment>

</navigation>

and here main.xml

<action android:id="@+id/action_to_authenticationFlow"
        app:destination="@id/loginFlow" >
        <argument
            android:name="uri"
            app:argType="string"
            app:nullable="true" />
    </action>

I've modified to this code but also does not work

 val args: AuthenticationFragmentArgs by navArgs()

    private fun handleDeepLinks() {

        val uriString = arguments?.getString("uri")
        Toast.makeText(context, uriString, Toast.LENGTH_LONG).show()

And I am using Driections class from action_to_authenticationFlow

val navController = findNavController(R.id.navHostFragment)
        val action = DashboardFragmentDirections.actionToAuthenticationFlow(uriString)
        navController.navigate(action)
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143

1 Answers1

0

You navigation should look like this:

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.chenriquevz.pokedex.ui.home.HomeFragment"
        tools:layout="@layout/fragment_home"
        android:label="@string/app_name">

        <action
            android:id="@+id/home_to_pokemon"
            app:destination="@+id/navigation_pokemon">
            <argument
                android:name="pokemonID"
                app:argType="string"
                app:nullable="false" />
        </action>

    </fragment>


    <fragment
        android:id="@+id/navigation_pokemon"
        android:name="com.chenriquevz.pokedex.ui.pokemon.PokemonFragment"
        android:label="@string/app_name"
        tools:layout="@layout/fragment_pokemon">
        <argument
            android:name="pokemonID"
            app:argType="string"
            app:nullable="false" />
     </fragment>

and your destination Fragment should look like this:

    private val args: PokemonFragmentArgs by navArgs()

    val pokemonID= args.uri

And your navigation should look like this, note that the direction has its name derivated from the origin Fragment:

val action = HomeFragmentDirections.homeToPokemon(query)
navController.navigate(action)

After update your navigation graph sometimes it may require a build/clean project to generate the proper direction.

Henrique Vasconcellos
  • 1,144
  • 1
  • 8
  • 13
  • I have this but it doesn't work for me. I paste code where is may problem I think – Michał Ziobro May 12 '20 at 16:19
  • in your example you are not using directions to navigate/pass argument, are you? Do you have the depency added to your gradle at project level? – Henrique Vasconcellos May 12 '20 at 16:22
  • Yes but I think this bundles should, work. In the meantime I've changed to this directions arguments but this doesn't work. I think If I have this in one xml it will work ok but in both methods, but the problem is that this login.xml is included to main.xml and I am login to nested flow. I consider whether it is possible to pass to nested flow – Michał Ziobro May 12 '20 at 16:24
  • I tested args.uri on debugger so please do not suggest that there is old arguments.getString() – Michał Ziobro May 12 '20 at 16:25
  • I have found two resources that may help you, as I do not have the time to replicate what you are doing. https://stackoverflow.com/questions/54477573/passing-arguments-to-a-nested-navigation-architecture-component-graph https://issuetracker.google.com/issues/109505019 (they provide some workaround here, you will have to test it) I just noticed that my code has args.String("whatever") and it should have been args.uri as you mentioned. – Henrique Vasconcellos May 12 '20 at 16:43
  • also, I just noticed that I have a code very similar to yours. Try to make it app:nullable="false" *or* android:defaultValue="@null" (I think it has a high chance to help you). – Henrique Vasconcellos May 12 '20 at 16:46
  • It doesn't help, it do not want to pass args to nested graph startDestination from global action in parent graph, both by bundle and generated Directions/Args classes as this is URI i just set it globally on my App.INSTANCE.lastUri variable and code works, maybe in future I found better solution, I also do not want to move loginFlow back to parent graph. – Michał Ziobro May 12 '20 at 17:06