Android Navigation Component pass param to drawer nav entry

203 views Asked by At

I'm using the android navigation component with a drawer menu. My toolbar contains a spinner element. I've have only access to the spinner-element on MainActivity.java. Now I would like to pass the user selection of the spinner to a drawermenu item.

NavController (in MainActivity.java)

    // set up navigation
    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupWithNavController(binding.navView, navController);
    // top level dest
    Set<Integer> topLevelDest = new HashSet<>();
    topLevelDest.add(R.id.nav_foo);
    topLevelDest.add(R.id.nav_bar);

    appBarConfiguration =
            new AppBarConfiguration.Builder(topLevelDest).setDrawerLayout(binding.drawerLayout)
                    .build();
    NavigationUI.setupWithNavController(binding.toolbar.toolbar, navController, appBarConfiguration);

Here is my MainActivity.xml which includes a Drawerlayout, NavHostFragment, and CustomToolbar:

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/drawer_navigation" />
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

And here is my Navigationgraph:

<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/drawer_navigation"
app:startDestination="@id/nav_foo">
<fragment
    android:id="@id/nav_foo"
    android:name="FooFragment"
    tools:layout="@layout/fragment_foo"/>
<fragment
    android:id="@id/nav_bar"
    android:name="BarFragment">
</fragment>
<fragment
    android:id="@+id/nav_A"
    android:name="AFragment"
    tools:layout="@layout/fragment_A" />
</navigation>

I can acces the spinner in MainActivity via binding.toolbar.spinner

Now I would like to pass the selection of the spinner (int) in the toolbar to the fragment_A, which is also a DrawerNav-Entry but no toplevel destination.

Is this even possible?

I temporarly solved it over shared-preferences

Regards

0

There are 0 answers