0

In a small demo project for Android on Kotlin applied standard navigation with parameter passing

id ("androidx.navigation.safeargs.kotlin") version "2.5.3" apply false implementation("androidx.navigation:navigation-fragment-ktx:2.5.3") implementation("androidx.navigation:navigation-ui-ktx:2.5.3")

in the navigation file nav_graph.xml:

    app:startDestination="@id/SkySearchFragment">
  
    <fragment
        android:id="@+id/SkySearchFragment"
        android:name="com.dinadurykina.skylexicon.ui.search.SkySearchFragment"
        android:label="@string/bar_search_translate"
        tools:layout="@layout/fragment_sky_search">
    ......
        <argument
            android:name="sslovo"
            app:argType="string"
            android:defaultValue=" " />
        ....
    </fragment>
   <fragment
        android:id="@+id/SkyWordsFragment"
        android:name="com.dinadurykina.skylexicon.ui.words.SkyWordsFragment"
        android:label="@string/bar_search_words"
        tools:layout="@layout/fragment_sky_words">
       .....
        <argument
            android:name="bslovo"
            android:defaultValue="monitor"
            app:argType="string" />
        .....
    </fragment>

those. at the fragment SkySearchFragment there is an argument "sslovo" с defaultValue=" "

also in fragment SkyWordsFragment there is an argument "bslovo" с defaultValue="monitor"

Both fragments are called from the curtain, and there is no standard parameter passing mechanism. Navigation Architecture Component- Passing argument data to the startDestination Trying to override the default value for "sslovo" and "bslovo" at runtime: AppCompatActivity():

Variant 1:

    91  navController.setGraph(R.navigation.nav_graph, bundleOf("sslovo" to skyStartSlovoSearch)) 
    //  Works - overrides sslovo
    92  navController.setGraph(R.navigation.nav_graph, bundleOf("bslovo" to skyStartSlovoBase))   
    // NOT working, sometimes crash at start

Wherein : if I indicate only 91 pages, then the word will come, if I specify only 92 pages, then the word DOES NOT COME, if I specify pages 91 and 92, then the word DOES NOT come and the word DOES NOT come and sometimes crashes at the start

Variant 2:

    val bundle = Bundle()
        bundle.putString("sslovo", skyStartSlovoSearch)  
        // those. the first argument comes and the second     argument doesn't come
        bundle.putString("bslovo", skyStartSlovoBase)  // doesn't crash, but doesn't work either
        navController.setGraph(R.navigation.nav_graph, bundle)  // come from defaultValue from xml

those. the first argument sslovo comes and the second bslovo does not come stacking order in Bundle() does not affect - still only sslovo

Variant 3: the order of fragments in nav_graph.xml does not affect the above patterns IF you specify the second fragment as the start fragment (with the bslovo argument)

    app:startDestination="@id/SkyWordsFragment"

THEN then bslovo comes and sslovo stops coming

    navController.graph.addInDefaultArgs(bundle) 

// does not work !!!! - just does nothing with a completely cryptic explanation: Concatenates the default arguments for this destination with arguments provided to create the final set of arguments, which should I use to go to this destination.

There is also addArgument and removeArgument, but you should not add, but redefine the value of the argument. In general, it is written that setGraph rebinds the specified xml to the navigation and sends the START bandle And how else can you change non-starting defaultValue without rebinding or somehow else? Can override onGraphCreated(startDestinationArgs: Bundle) but it is private and how to reassign defaultValue there

Conclusion: setGraph sends an argument only to the fragment specified by the start fragment in nav_graph.xml

for some reason, when accessing arguments, there is no indication of the fragment to which the argument refers

Can someone please explain how to set defaultValue value in NON-START fragment nav_graph.xml from code?

ASDzendo
  • 31
  • 1
  • If it isn't the start destination of your graph, then all arguments are the responsibility of whoever is calling `navigate` to go to that destination. Why are you using arguments (or arguments with default values) at all for something you aren't passing in when you navigate to that destination? – ianhanniballake Apr 14 '23 at 20:38

0 Answers0