title: Android Navigation Component - Navigate up opens the same fragment
body: <p>I'm having a problem where when executing</p>
  
  <pre class="lang-java prettyprint-override"><code>findNavController(R.id.main_nav_host).navigateUp()
  </code></pre>
  
  <p>or</p>
  
  <pre class="lang-java prettyprint-override"><code>findNavController(R.id.main_nav_host).popBackStack()
  </code></pre>
  
  <p>Instead of going back to the last fragment in the backstack, it reopens/navigates to the same/current fragment.</p>
  
  <p>Can somebody point me in the right direction why this is happening?</p>
  
  <p>Navigation graph:  </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;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/main_navigation_root"
              app:startDestination="@+id/dest_main"&gt;
  
      &lt;fragment
              android:id="@+id/dest_main"
              android:name="com.example.popularmovies.ui.main.views.MainMoviesFragment"
              android:label="@string/home"
              tools:layout="@layout/fragment_main_movies"&gt;
  
          &lt;action
                  android:id="@+id/action_dest_main_to_dest_movie_details"
                  app:destination="@+id/dest_movie_details"
                  app:enterAnim="@anim/slide_in_right"
                  app:exitAnim="@anim/slide_out_left"
                  app:popEnterAnim="@anim/slide_in_left"
                  app:popExitAnim="@anim/slide_out_right" /&gt;
  
      &lt;/fragment&gt;
  
      &lt;fragment
              android:id="@+id/dest_movie_details"
              android:name="com.example.popularmovies.ui.details.movie.view.MovieDetailsFragment"
              android:label="@string/movie_details"
              tools:layout="@layout/fragment_movie_details"/&gt;
  
  &lt;/navigation&gt;
  </code></pre>
  
  <p>MainActivity layout:  </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;FrameLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"&gt;
  
      &lt;fragment
              android:id="@+id/main_nav_host"
              android:name="androidx.navigation.fragment.NavHostFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:defaultNavHost="true"
              app:navGraph="@navigation/main_navigation"/&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>MainActivity:  </p>
  
  <pre><code>class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {
  
      @Inject
      lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector&lt;Fragment&gt;
  
      private lateinit var appBarConfiguration: AppBarConfiguration
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          setSupportActionBar(toolbar)
  
          initNavUi()
      }
  
      override fun onBackPressed() {
  
          findNavController(R.id.main_nav_host).popBackStack()
      }
  
      override fun onCreateOptionsMenu(menu: Menu): Boolean {
          // Inflate the menu; this adds items to the action bar if it is present.
          menuInflater.inflate(R.menu.menu_main, menu)
          return true
      }
  
      override fun onOptionsItemSelected(item: MenuItem): Boolean {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          return when (item.itemId) {
              R.id.action_settings -&gt; true
              else -&gt; super.onOptionsItemSelected(item)
          }
      }
  
      override fun onSupportNavigateUp(): Boolean {
  
          return findNavController(R.id.main_nav_host).navigateUp()
      }
  
      override fun supportFragmentInjector(): AndroidInjector&lt;Fragment&gt; {
  
          return dispatchingAndroidInjector
      }
  
      private fun initNavUi() {
  
          val navController = Navigation.findNavController(this, R.id.main_nav_host)
          appBarConfiguration = AppBarConfiguration(
              setOf(R.id.dest_main)
          )
  
          NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
      }
  
  }
  </code></pre>
  
  <p>Destination home fragment:  </p>
  
  <pre><code>class MainMoviesFragment : Fragment(), Injectable, MovieViewHolder.MovieClickListener {
  
      @Inject
      lateinit var viewModelFactory: ViewModelProvider.Factory
  
      private lateinit var fragmentViewModel: MainMoviesFragmentViewModel
  
      private lateinit var moviesRv: RecyclerView
      private lateinit var moviesAdapter: MainMoviesAdapter
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  
          return inflater.inflate(R.layout.fragment_main_movies, container, false)
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  
          initViews(view)
      }
  
      override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
  
          fragmentViewModel = ViewModelProviders.of(this,viewModelFactory).get(MainMoviesFragmentViewModel::class.java)
          fragmentViewModel.start()
  
          observe()
      }
  
      override fun onMovieClicked(position: Int) {
  
          fragmentViewModel.onMovieClicked(position)
      }
  
      private fun initViews(view: View) {
  
          moviesRv = view.findViewById&lt;RecyclerView&gt;(R.id.fragment_main_movies_rv).apply{
  
              layoutManager = LinearLayoutManager(context)
              setHasFixedSize(true)
  
              moviesAdapter = MainMoviesAdapter(this@MainMoviesFragment)
              adapter = moviesAdapter
          }
      }
  
      private fun observe() {
  
          fragmentViewModel.moviesLiveData.observe(this, Observer { moviesAdapter.submitList(it) })
          fragmentViewModel.onMovieClickedLiveEvent.observe(this, Observer { handleMovieClickedEvent(it) })
  
      }
  
      private fun handleMovieClickedEvent(movieModel: MovieModel?){
  
          val action = MainMoviesFragmentDirections.actionDestMainToDestMovieDetails()
          findNavController().navigate(action)
      }
  
  }
  </code></pre>
  
  <p>Destination target fragment:  </p>
  
  <pre><code>class MovieDetailsFragment : Fragment() {
  
      private lateinit var viewModel: MovieDetailsFragmentViewModel
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  
          return inflater.inflate(R.layout.fragment_movie_details, container, false)
      }
  
      override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
  
          viewModel = ViewModelProviders.of(this).get(MovieDetailsFragmentViewModel::class.java)
      }
  
  }
  </code></pre>
  
  <p>The project code on GitHub can be found <a href="https://github.com/royhenengel/PopularMovies" rel="noreferrer">here</a></p>