title: Android: Navigation Component - How to connect Toolbar menu to the fragments?
body: <p>Iv been stuck in trying to add the Navigation Component to my app. Specifically to my toolbar menu items.</p>
  <p>I have been following the <a href="https://developer.android.com/guide/navigation/navigation-migrate#move" rel="nofollow noreferrer">navigation migrate</a> documentation on Android's site but I am getting a but confused on how it works or even to set it up.</p>
  <p>This is a <strong>single-activity multi-fragment</strong> architecture.</p>
  <p>The app would start in the MainFragment and then when tapping the menu items in the toolbar, such as Main Frag to Search Movie Frag. The user would be able to navigate using the menu items from anywhere in the app (e.g. if they click the home icon, they should be sent to the home screen form anywhere in the app. Still thinking if thats how it should be done).</p>
  <p>The main thing is I don't know how to properly attach the Navigation Component to the menu items.</p>
  <p><strong>App</strong></p>
  <p><a href="https://i.stack.imgur.com/0QvB2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0QvB2.png" alt="Main Fragment displayed on phone" /></a></p>
  <p><strong>Nav Graph</strong></p>
  <p><a href="https://i.stack.imgur.com/efPuh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/efPuh.png" alt="Nav Graph displayed on Android Studio" /></a></p>
  <p><strong>nav_graph.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:id=&quot;@+id/nav_graph&quot;
      app:startDestination=&quot;@id/mainFragment&quot;&gt;
  
      &lt;fragment
          android:id=&quot;@+id/mainFragment&quot;
          android:name=&quot;com.example.movieapp.ui.main.MainFragment&quot;
          android:label=&quot;MainFragment&quot;&gt;
          &lt;action
              android:id=&quot;@+id/action_mainFragment_to_searchMovieFragment&quot;
              app:destination=&quot;@id/searchMovieFragment&quot; /&gt;
      &lt;/fragment&gt;
      &lt;fragment
          android:id=&quot;@+id/searchMovieFragment&quot;
          android:name=&quot;com.example.movieapp.ui.search.SearchMovieFragment&quot;
          android:label=&quot;SearchMovieFragment&quot; /&gt;
  &lt;/navigation&gt;
  </code></pre>
  <p><strong>main_activity.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:id=&quot;@+id/container&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.activity.MainActivity&quot;&gt;
  
      &lt;include
          layout=&quot;@layout/appbar&quot;
          android:id=&quot;@+id/mainToolBar&quot;/&gt;
  
      &lt;androidx.fragment.app.FragmentContainerView
          android:id=&quot;@+id/nav_host_fragment&quot;
          android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;0dp&quot;
          app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
          app:layout_constraintRight_toRightOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@id/mainToolBar&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:defaultNavHost=&quot;true&quot;
          app:navGraph=&quot;@navigation/nav_graph&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p><strong>MainActivity.kt</strong></p>
  <pre><code>class MainActivity : AppCompatActivity() {
  
      private lateinit var toolbar: Toolbar
      private val navController by lazy {
          (supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment).navController
      }
      private lateinit var appBarConfiguration: AppBarConfiguration
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.main_activity)
          toolbar = findViewById(R.id.mainToolBar)
          setSupportActionBar(toolbar)
          appBarConfiguration = AppBarConfiguration(navController.graph, null)
          setupActionBarWithNavController(navController, appBarConfiguration)
      }
  
      override fun onCreateOptionsMenu(menu: Menu?): Boolean {
          val inflater: MenuInflater = menuInflater
          inflater.inflate(R.menu.main_menu_bar, menu)
          return true
      }
  
      override fun onSupportNavigateUp(): Boolean {
          return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
      }
  }
  </code></pre>
  <p><strong>MainFragment.kt</strong></p>
  <pre><code>class MainFragment : Fragment() {
  
      companion object {
          fun newInstance() = MainFragment()
      }
  
      private lateinit var viewModel: MainViewModel
      private lateinit var binding: MainFragmentBinding
      private lateinit var navController: NavController
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
          binding = DataBindingUtil.inflate(inflater, R.layout.main_fragment, container, false)
          setHasOptionsMenu(true)
          return binding.root
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          navController = Navigation.findNavController(view)
  //        navController.navigate(R.id.action_mainFragment_to_searchMovieFragment)
      }
  
      override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
          viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
          binding.viewModel = viewModel
          // TODO: Use the ViewModel
      }
  
  }
  </code></pre>
  <p><strong>main_fragment.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
  
      &lt;data&gt;
          &lt;variable
              name=&quot;viewModel&quot;
              type=&quot;com.example.movieapp.ui.main.MainViewModel&quot; /&gt;
      &lt;/data&gt;
  
      &lt;LinearLayout
          android:orientation=&quot;vertical&quot;
          android:id=&quot;@+id/main_fragment&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          tools:context=&quot;.ui.main.MainFragment&quot;&gt;
  
          &lt;TextView
              android:id=&quot;@+id/message&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:text=&quot;MainFragment1&quot; /&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/layout&gt;
  </code></pre>