0

There is such a method in the activity that responsible to open the fragments by Uri

...
    fun navigate(uri: Uri, savedInstanceState: Bundle?) {
        with(supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment) {
            val authGraph: NavGraph = navController.navInflater.inflate(R.navigation.auth_nav_graph)
            maybeSetIapNavGraphs(navHostFragment.navController, authGraph)
            navController.graph = authGraph

            if (savedInstanceState == null) {
                navController.popBackStack()
                navController.navigate(uri)
            }
        }
    }
...

I need to pass a boolean value to the fragment that is going to be opened, however, I see that navigate doesn't take Bundle as a param, and the only available constructor with Uri is .navigate(uri, NavOptions), so looks like there is no way to pass a bundle.

The question is - how to pass a boolean value to the fragment?

UPD

I also tried to do it like this:

...
    fun navigate(uri: Uri, savedInstanceState: Bundle?) {
        with(supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment) {
            val authGraph: NavGraph = navController.navInflater.inflate(R.navigation.auth_nav_graph)
            maybeSetIapNavGraphs(navHostFragment.navController, authGraph)

            val bundle = Bundle().apply { putInt("isEnabled", 1) }
            navController.setGraph(authGraph, bundle)

            if (savedInstanceState == null) {
                navController.popBackStack()
                navController.navigate(uri)
            }
        }
    }
...

However, when I try to get this value in the fragment like

val arg1Value = requireArguments().getInt("isEnabled")

it gives 0

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121

1 Answers1

0

Yes, it is possible. Define support of extra argument in the xml config:

<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/mobile_navigation"
    app:startDestination="@+id/navigation_dashboard">

    <fragment
        android:id="@+id/navigation_home"
        android:name="ru.home.government.screens.deputies.DeputiesFragment"
        android:label="@string/title_deputies"
        tools:layout="@layout/fragment_deputies">
        <argument
            android:name="EXTRA_NO_BOTTOM_VIEW"
            app:argType="boolean"
            android:defaultValue="false" />

    </fragment>
...

After it should be possible to pass data within a Bundle over nav controller. Fore more reference check this and this.

Gleichmut
  • 5,953
  • 5
  • 24
  • 32