title: Android NavigationUI - Handle backpress when starting fragment is poped out
body: <p>I am using <a href="https://developer.android.com/reference/androidx/navigation/ui/NavigationUI" rel="nofollow noreferrer">NavigationUI</a> of android architecture components with bottom navigation in my android app. I have the following situation </p>
  
  <ol>
  <li>The app starts at Fragment <strong>A</strong></li>
  <li>Initial calculation is done and now I pop out Fragment <strong>A</strong> by <code>navController.popBackStack()</code></li>
  <li>The app goes to Fragment <strong>B</strong> (This serves as a 'Home' for the app)</li>
  <li>Now there are more fragments say <strong>C</strong> and <strong>D</strong></li>
  <li>The user can navigate between <strong>B</strong>, <strong>C</strong> and <strong>D</strong></li>
  </ol>
  
  <p>Now the problem is in my navigation graph, my starting fragment was <strong>A</strong> (by defining <code>app:startDestination="@id/fragmentA"</code>) but that is now pop out of the activity. Due to this, anytime if I backpress, the app just closes instead going back to the previous fragment. For example, let's say I navigate to Fragment <strong>C</strong> or <strong>D</strong> from fragment <strong>B</strong> and if I press back button, my app will close instead of going to fragment <strong>B</strong>. If the there way to 'reassign' the <code>startDestination</code>?</p>
  
  <p>I checked the answer to <a href="https://stackoverflow.com/questions/51173002/how-to-change-start-destination-of-a-navigation-graph-programmatically-jetpack">this</a> question which has a similar situation as above but using <code>NavOptions</code> is somehow not working. Is there any other way? Following is the code I used to pop fragment and add nav options</p>
  
  <pre><code> navController.popBackStack()     
   val navOptions: NavOptions = NavOptions.Builder()
              .setPopUpTo(R.id.homeFragment, true)
              .build()
  
   navController.navigate(R.id.action_fragmentA_to_fragmentB, null, navOptions)
  </code></pre>