title: Add back arrow to fragment with Navigation Component
body: <p>I implemented a basic Navigation component on an app that consist of a <strong>MainActivity</strong> that holds a <strong>Toolbar</strong> (that I have added to have the back arrow functionality) and a fragment container which starts with <strong>Fragment A</strong>. In this fragment I have a button which redirects to a blank <strong>Fragment B</strong>.</p>
  
  <p>I can return to <strong>fragment A</strong> (from <strong>fragment B</strong>) from the bottom Android navigation using Navigation component, but I want to do the same using the back arrow from the toolbar that I have added. 
  I implemented the arrow  putting  <em>setSupportActionBar(findViewById(R.id.toolbar))</em> in <strong>Main Activity</strong> and <em>(activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true)</em> on <strong>Fragment B</strong>, but when I tap it it doesn't redirect to <strong>Fragment A</strong> because (I suppose) that the back stack is empty.</p>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>class MainActivity : AppCompatActivity() {
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      setSupportActionBar(findViewById(R.id.toolbar))
  }
  </code></pre>
  
  <p><strong>MainActivity xml</strong></p>
  
  <pre><code>&lt;androidx.appcompat.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="?attr/colorPrimary"
      android:minHeight="?attr/actionBarSize"
      android:theme="?attr/actionBarTheme"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" /&gt;
  
  &lt;androidx.fragment.app.FragmentContainerView
      android:id="@+id/nav_host_fragment"
      android:name="androidx.navigation.fragment.NavHostFragment"
      android:layout_width="0dp"
      android:layout_height="0dp"
      app:defaultNavHost="true"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/toolbar"
      app:navGraph="@navigation/nav_graph" /&gt;
  </code></pre>
  
  <p><strong>FragmentB</strong></p>
  
  <pre><code> override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
      (activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true)
  }
  </code></pre>
  
  <p>Added this to remove the default toolbar in <strong>styles.xml</strong></p>
  
  <pre><code>&lt;item name="windowNoTitle"&gt;true&lt;/item&gt;
  </code></pre>
  
  <p>In <strong>FragmentB</strong> up button not working
  <a href="https://i.stack.imgur.com/se0bu.png" rel="nofollow noreferrer">fragmentB with back not working</a></p>
  
  <p>I want to do that using <strong>Navigation Component</strong>, any ideas?</p>
  
  <p>Thanks.</p>