title: Android: How do I get the Settings menu to appear with NavigationUI?
body: <p>I'm trying to follow this: <a href="https://developer.android.com/guide/navigation/navigation-ui" rel="nofollow noreferrer">https://developer.android.com/guide/navigation/navigation-ui</a> along with a review of <a href="https://codelabs.developers.google.com/codelabs/android-navigation/#8" rel="nofollow noreferrer">https://codelabs.developers.google.com/codelabs/android-navigation/#8</a> (although the second link is in Kotlin and I'm coding in Javascript) to implement an App in Android with navigation drawer on the left and a settings button (preferences) on the top right.</p>
  
  <p>I'm using the NavigationUI with fragments for each option from the drawer and the settings.</p>
  
  <p>My problem is that my settings fragment is not appearing. I think I'm nearly there, but despite reviewing several articles and questions cannot get it to work.</p>
  
  <p>This code allows me to switch fragment from the nav drawer:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      DrawerLayout drawer = findViewById(R.id.drawer_layout);
      NavigationView navigationView = findViewById(R.id.nav_view);
      mAppBarConfiguration = new AppBarConfiguration.Builder(
              R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
              .setDrawerLayout(drawer)
              .build();
      NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
      NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
      NavigationUI.setupWithNavController(navigationView, navController);
      //NavigationUI.setupWithNavController(toolbar, navController, mAppBarConfiguration);
  }
  </code></pre>
  
  <p>If I switch the commented code on the last two lines (so I execute the last line but not second to last), to match the guide then the switching of fragment fails.</p>
  
  <p>My code to handle the Settings menu is:</p>
  
  <pre><code>@Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // 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.
  
      NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
      return NavigationUI.onNavDestinationSelected(item, navController)
              || super.onOptionsItemSelected(item);
  /*
      int id = item.getItemId();
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          // TODO: Do something to accept settings
          View contextView =  findViewById(R.id.nav_host_fragment); // Apparently any layout can be used
          Snackbar.make(contextView, "Settings", Snackbar.LENGTH_LONG)
                  .setAction("Action", null).show();
          //changeFragment(new SettingsFragment());
          getSupportFragmentManager()
                  .beginTransaction()
                  .replace(R.id.nav_host_fragment, new SettingsFragment(), "nh")
                  .addToBackStack("nh")
                  .commit();
          return true;
      }
      return super.onOptionsItemSelected(item);
  */
  }
  </code></pre>
  
  <p>The active portion matches the guide above, is getting called (proved by setting a breakpoint) but doing nothing.</p>
  
  <p>If I alternatively switch this for the commented out code instead, then my settings fragment does appear but overlays the current fragment. I understand from another answer that this is because I am using two different methods to handle fragment visibility, which makes sense and is why I am trying to get my settings fragment to be handled by the NavigationUI framework.</p>
  
  <p>I feel that it must be quite simple to cure, but having read and re-read the guide (and lots of answers cannot make it work)</p>