title: Navigation graph with multiple top level destinations and back-key behaviors
body: <p>When I have multiple top level destinations in my navigation graph, and when I click back-key to finish my app, those destinations other than the <code>startDestination</code> won't finish but pop the <code>startDestination</code> Fragment.</p>
  <ul>
  <li>FragmentA (startDestination) -&gt; finish immediately with back-key</li>
  <li>FragmentB -&gt; back to FragmentA with back-key</li>
  <li>FragmentC -&gt; back to FragmentA with back-key</li>
  </ul>
  <p>I've tried <code>app:popUpTo=&quot;@id/nav_graph&quot;</code> and/or <code>app:popUpToInclusive=&quot;true&quot;</code> but in vain.</p>
  <p><strong>Activity:</strong></p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main_activity);
  
          NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  
          DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
  
          AppBarConfiguration appBarConfiguration =
                  new AppBarConfiguration.Builder(R.id.fragmentA, R.id.fragmentB, R.id.fragmentC)
                          .setOpenableLayout(drawerLayout)
                          .build();
  
          NavigationView navView = findViewById(R.id.nav_view);
          NavigationUI.setupWithNavController(navView, navController);
  
          Toolbar toolbar = findViewById(R.id.toolbar);
          NavigationUI.setupWithNavController(
                  toolbar,
                  navController,
                  appBarConfiguration
          );
      }
  }
  </code></pre>
  <p><strong>layout.xml:</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;!-- Use DrawerLayout as root container for activity --&gt;
  &lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:id=&quot;@+id/drawer_layout&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:fitsSystemWindows=&quot;true&quot;&gt;
  
      &lt;!-- Layout to contain contents of navigation_view body of screen (drawer will slide over this) --&gt;
      &lt;androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;&gt;
  
          &lt;androidx.appcompat.widget.Toolbar
              android:id=&quot;@+id/toolbar&quot;
              android:layout_width=&quot;0dp&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:background=&quot;?attr/colorPrimary&quot;
              android:minHeight=&quot;?attr/actionBarSize&quot;
              android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;
              app:layout_constraintEnd_toEndOf=&quot;parent&quot;
              app:layout_constraintStart_toStartOf=&quot;parent&quot;
              app:layout_constraintTop_toTopOf=&quot;parent&quot;
              app:popupTheme=&quot;@style/AppTheme.PopupOverlay&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_constraintEnd_toEndOf=&quot;parent&quot;
              app:layout_constraintStart_toStartOf=&quot;parent&quot;
              app:layout_constraintTop_toBottomOf=&quot;@id/toolbar&quot;
              app:navGraph=&quot;@navigation/nav_graph&quot; /&gt;
      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
      &lt;!-- Container for contents of drawer - use NavigationView to make configuration easier --&gt;
      &lt;com.google.android.material.navigation.NavigationView
          android:id=&quot;@+id/nav_view&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;match_parent&quot;
          android:layout_gravity=&quot;start&quot;
          android:fitsSystemWindows=&quot;true&quot;
          app:headerLayout=&quot;@layout/navigation_header&quot;
          app:menu=&quot;@menu/navigation_view&quot; /&gt;
  
  &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;
  </code></pre>
  <p><strong>nav_graph:</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;
      android:id=&quot;@+id/nav_graph&quot;
      app:startDestination=&quot;@id/fragmentA&quot;&gt;
  
      &lt;fragment
          android:id=&quot;@+id/fragmentA&quot;
          android:name=&quot;com.stackoverflow.FragmentA&quot;
          android:label=&quot;@string/fragmentA&quot;&gt;
          &lt;argument
              android:name=&quot;message&quot;
              android:defaultValue=&quot;(no message)&quot;
              app:argType=&quot;string&quot; /&gt;
      &lt;/fragment&gt;
  
      &lt;action
          android:id=&quot;@+id/toFragmentAWithMessage1&quot;
          app:destination=&quot;@id/fragmentA&quot;&gt;
          &lt;argument
              android:name=&quot;message&quot;
              android:defaultValue=&quot;message 1&quot;
              app:argType=&quot;string&quot; /&gt;
      &lt;/action&gt;
  
      &lt;action
          android:id=&quot;@+id/toFragmentAWithMessage2&quot;
          app:destination=&quot;@id/fragmentA&quot;&gt;
          &lt;argument
              android:name=&quot;message&quot;
              android:defaultValue=&quot;message 2&quot;
              app:argType=&quot;string&quot; /&gt;
      &lt;/action&gt;
  
      &lt;fragment
          android:id=&quot;@+id/fragmentB&quot;
          android:name=&quot;com.stackoverflow.navigationui.FragmentB&quot;
          android:label=&quot;@string/fragmentB&quot;&gt;
          &lt;argument
              android:name=&quot;message&quot;
              android:defaultValue=&quot;(no message)&quot;
              app:argType=&quot;string&quot; /&gt;
      &lt;/fragment&gt;
  
      &lt;action
          android:id=&quot;@+id/toFragmentBWithMessage1&quot;
          app:destination=&quot;@id/fragmentB&quot;&gt;
          &lt;argument
              android:name=&quot;message&quot;
              android:defaultValue=&quot;message 1&quot;
              app:argType=&quot;string&quot; /&gt;
      &lt;/action&gt;
  
      &lt;action
          android:id=&quot;@+id/toFragmentBWithMessage2&quot;
          app:destination=&quot;@id/fragmentB&quot;&gt;
          &lt;argument
              android:name=&quot;message&quot;
              android:defaultValue=&quot;message 2&quot;
              app:argType=&quot;string&quot; /&gt;
      &lt;/action&gt;
  
      &lt;fragment
          android:id=&quot;@+id/fragmentC&quot;
          android:name=&quot;com.stackoverflow.FragmentC&quot;
          android:label=&quot;@string/fragmentC&quot;&gt;
          &lt;argument
              android:name=&quot;message&quot;
              android:defaultValue=&quot;(no message)&quot;
              app:argType=&quot;string&quot; /&gt;
      &lt;/fragment&gt;
  
      &lt;action
          android:id=&quot;@+id/toFragmentCWithMessage1&quot;
          app:destination=&quot;@id/fragmentC&quot;&gt;
          &lt;argument
              android:name=&quot;message&quot;
              android:defaultValue=&quot;message 1&quot;
              app:argType=&quot;string&quot; /&gt;
      &lt;/action&gt;
  
      &lt;action
          android:id=&quot;@+id/toFragmentCWithMessage2&quot;
          app:destination=&quot;@id/fragmentC&quot;&gt;
          &lt;argument
              android:name=&quot;message&quot;
              android:defaultValue=&quot;message 2&quot;
              app:argType=&quot;string&quot; /&gt;
      &lt;/action&gt;
  
  &lt;/navigation&gt;
  </code></pre>
  <p>I know overriding <code>OnBackPressedCallback</code> is a workaround.</p>
  <pre><code>public class FragmentB extends Fragment {
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          
          requireActivity().getOnBackPressedDispatcher().addCallback(new OnBackPressedCallback(true) {
              @Override
              public void handleOnBackPressed() {
                  remove();
                  requireActivity().finish();
              }
          });
      }
  }
  </code></pre>
  <p>Is there a more fundamental solution?</p>