12

I am trying to bind toolbar to navigation controller, for that I am using the following code:

NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))

and in the menu file, I provided the id of the fragment to which app should navigate like this:

<item
    android:id="@+id/menuFragment"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />

and I have a simple navigation graph file like this:

<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/nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.vapoyan.myapplication.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_homeFragment_to_menuFragment"
            app:destination="@id/menuFragment" />
    </fragment>

    <fragment
        android:id="@+id/menuFragment"
        android:name="com.vapoyan.myapplication.MenuFragment"
        android:label="fragment_menu"
        tools:layout="@layout/fragment_menu" />
</navigation>

Does anyone have experience or can suggest how I can fix the issue and is toolbar supported by the navigation component?

Any example code or reference?

4 Answers4

13

Finally, I found a solution thanks to Google CodeLab

What I was missing was:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
     return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host))
            || super.onOptionsItemSelected(item)
}

Plus if you want to have back button support you need in onCreate method to add:

NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))

Basically, In my understanding just bt providing id of the correct fragment for the menu item and calling setupWithNavController should work, but that assumption was not correct, or maybe in the current version (1.0.0-alpha07) Google guys changed something. So now it is working fine.

If you see that there is a way to do it shorter :) or better :) let me know.

  • It is a side mention in the docs but the default behavior is for menu navigation with `NavigationUI.onNavDestinationSelected()` to wipe your backstack. To fix this you need to set `android:menuCategory="secondary"` on the menu items XML – smdufb Oct 15 '19 at 19:20
10

In addition to the above answer. Make sure the id's for both the navigation and item match.

android:id="@+id/menuFragment"

I got hung up on this because I had separate id's for each element.

tzg
  • 616
  • 1
  • 8
  • 17
2

I am using a Navigation drawer in my app, but i think this will be enough for ur case:

At this moment i am using this android libs: In my build.gradle

 implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha07'
 implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha07'

In my Activity

class MainActivity : BaseActivity() {

    @SuppressLint("RestrictedApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        val navController = Navigation.findNavController(this, /*ur nav controller ID*/)

        // Set up ActionBar
        setSupportActionBar(/*ur toolbar*/)

        NavigationUI.setupActionBarWithNavController(this, navController)
    }
}

U can check the docs here u can find more info: https://developer.android.com/topic/libraries/architecture/navigation/navigation-ui#create_a_toolbar

Catluc
  • 1,775
  • 17
  • 25
  • 1
    I found a solution I was missing call to `override fun onOptionsItemSelected(item: MenuItem): Boolean { return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host)) || super.onOptionsItemSelected(item) }` –  Nov 23 '18 at 09:57
0

For people using Kotlin, refer to this documentation link - https://developer.android.com/guide/navigation/navigation-ui#create_a_toolbar

  1. Define a toolbar in MainActivity.xml -
<LinearLayout>
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar" />
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        ... />
    ...
</LinearLayout>
  1. Call setupWithNavController() in MainActivity.kt onCreate()
override fun onCreate(savedInstanceState: Bundle?) {
    setContentView(R.layout.activity_main)

    ...

    val navController = findNavController(R.id.nav_host_fragment)
    val appBarConfiguration = AppBarConfiguration(navController.graph)
    findViewById<Toolbar>(R.id.toolbar)
        .setupWithNavController(navController, appBarConfiguration)
}
Smirky
  • 187
  • 2
  • 9