title: Handle physical back button in Navigation Component with Toolbar
body: <p>I have a simple proof of concept app that contains only 2 fragments: <code>MainFragment</code> and <code>RepositoryFragment</code> and 1 activity: <code>MainActivity</code></p>
  <p>Here's my <code>nav_graph</code></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/id_main&quot;&gt;
  
      &lt;fragment
          android:id=&quot;@+id/id_main&quot;
          android:name=&quot;com.repofetcher.main.view.MainFragment&quot;
          android:label=&quot;@string/app_name&quot;
          tools:layout=&quot;@layout/main_fragment&quot;&gt;
          &lt;action
              android:id=&quot;@+id/action_mainfragment_to_repositoryfragment&quot;
              app:destination=&quot;@id/id_repo&quot;
              app:popUpTo=&quot;@id/id_main&quot;
              app:popUpToInclusive=&quot;true&quot;/&gt;
      &lt;/fragment&gt;
  
      &lt;fragment
          android:id=&quot;@+id/id_repo&quot;
          android:name=&quot;com.repofetcher.repo.view.RepositoryFragment&quot;
          android:label=&quot;{arg_repo_name}&quot;
          tools:layout=&quot;@layout/repository_fragment&quot;&gt;
          &lt;argument
              android:name=&quot;arg_repo_name&quot;
              app:argType=&quot;string&quot;
              android:defaultValue=&quot;@null&quot;
              app:nullable=&quot;true&quot; /&gt;
      &lt;/fragment&gt;
  
  &lt;/navigation&gt;
  </code></pre>
  <p>MainActivity:</p>
  <pre><code>    private lateinit var navController: NavController
  
      override fun onCreate(savedInstanceState: Bundle?) {
          (application as RepoFetcherApplication).appComponent.mainComponent().create().inject(this)
          super.onCreate(savedInstanceState)
          setContentView(R.layout.main_activity)
          navController = findNavController(R.id.nav_host_fragment)
          setupActionBarWithNavController(navController)
      }
  
      override fun onSupportNavigateUp(): Boolean {
          return navController.navigateUp() || super.onSupportNavigateUp()
      }
  </code></pre>
  <p>Main activity's layout:</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:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.main.view.MainActivity&quot;&gt;
  
      &lt;fragment
          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:defaultNavHost=&quot;true&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
          app:layout_constraintRight_toRightOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          app:navGraph=&quot;@navigation/nav_graph&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p>This is the part that navigates from <code>MainFragment</code> to <code>RepositoryFragment</code></p>
  <pre><code>    private fun navToRepositoryFragment(name: String) {
          findNavController().navigate(MainFragmentDirections.actionMainfragmentToRepositoryfragment(argRepoName = name))
      }
  </code></pre>
  <p>RepositoryFragment:</p>
  <pre><code> override fun onCreate(savedInstanceState: Bundle?) {
          (requireActivity().application as RepoFetcherApplication).appComponent.repoComponent().create().inject(this)
          super.onCreate(savedInstanceState)
      }
  
      override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          dataBinding = DataBindingUtil.inflate(inflater, R.layout.repository_fragment, container, false)
          dataBinding.lifecycleOwner = this
          viewModel = ViewModelProvider(this, viewModelProviderFactory).get(RepositoryViewModel::class.java)
          dataBinding.viewModel = viewModel
          return dataBinding.root
      }
  
      override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
          viewModel.getRepository(args.argRepoName)
      }
  </code></pre>
  <p>Ideally, I would like to achieve these navigations:</p>
  <p>navigating from <code>MainFragment</code> to <code>RepositoryFragment</code>, then by clicking either the back button on the <code>Toolbar</code> or on the device, it should navigate back to the <code>MainFragment</code>.</p>
  <p>However in my case, only the back button on the toolbar will work as intended. The back button on the device will directly quit the app.</p>
  <p>I wonder what I'm missing in the code?</p>
  <h1>Update</h1>
  <p>One of the real problem behind this question is I used livedata for a single event (to handle the click event in order to navigate to <code>RepositoryFragment</code>).</p>
  <p>So after taking a look at this article :
  <a href="https://proandroiddev.com/livedata-with-single-events-2395dea972a8" rel="nofollow noreferrer">https://proandroiddev.com/livedata-with-single-events-2395dea972a8</a></p>
  <p>with solution @GabrieleMariotti provided and got the problem solved.</p>
  <p>I would like to thank @GabrieleMariotti who helped me with great patient.</p>