SetupWithNavController does't work for toolbar

11,546

Solution 1

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.

Solution 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

Share:
11,546
Admin
Author by

Admin

Updated on June 07, 2022

Comments

  • Admin
    Admin about 2 years

    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?

  • Admin
    Admin over 5 years
    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) }
  • mohammed yaseen
    mohammed yaseen over 4 years
    Thanks very much! This isn't seem to have been documented.
  • Dante
    Dante about 4 years
    Many Thanks tzg, You make my day.