title: Switch between Fragments with onNavigationItemSelected in new Navigation Drawer Activity template (Android Studio 1.4 onward)
body: <p>IntelliJ has made changes to the Navigation Drawer template Activity in Android Studio with fewer lines of code in the Activity class. The new Activity class looks like this:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                      .setAction("Action", null).show();
          }
      });
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
              this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
      drawer.setDrawerListener(toggle);
      toggle.syncState();
  
      NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
      navigationView.setNavigationItemSelectedListener(this);
  }
  
  @Override
  public void onBackPressed() {
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      if (drawer.isDrawerOpen(GravityCompat.START)) {
          drawer.closeDrawer(GravityCompat.START);
      } else {
          super.onBackPressed();
      }
  }
  
  
  @SuppressWarnings("StatementWithEmptyBody")
  @Override
  public boolean onNavigationItemSelected(MenuItem item) {
      // Handle navigation view item clicks here.
      int id = item.getItemId();
  
      if (id == R.id.nav_camara) {
          // Handle the camera action
      } else if (id == R.id.nav_gallery) {
  
      } else if (id == R.id.nav_slideshow) {
  
      } else if (id == R.id.nav_manage) {
  
      } else if (id == R.id.nav_share) {
  
      } else if (id == R.id.nav_send) {
  
      }
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      drawer.closeDrawer(GravityCompat.START);
      return true;
  }
  }
  </code></pre>
  
  <p>One of the most notable change here is the method:</p>
  
  <p><code>onNavigationItemSelected(MenuItem item)</code></p>
  
  <p>The old Navigation Drawer template's definition of this method was:</p>
  
  <p><code>onNavigationItemSelected(int position, long itemId)</code></p>
  
  <p>You could modify that old template by deleting the inner <code>PlaceHolderFragment</code> class, creating your own fragments and layouts and doing something like this:</p>
  
  <pre><code>Fragment fragment = null;
  switch (position) {
      case 0:
          fragment = new FragmentA();
          break;
      case 1:
          fragment = new FragmentB();
          break;
      default:
          break;
  }
  
  if (fragment != null) {
      FragmentManager fragmentManager = getSupportFragmentManager();
      fragmentManager.beginTransaction()
              .replace(R.id.frame_container, fragment).commit();
  
  }
  </code></pre>
  
  <p>But this doesn't work with the new template (at least not from my little knowledge). I have tried:</p>
  
  <pre><code> public boolean onNavigationItemSelected(MenuItem item) {
      // Handle navigation view item clicks here.
      int id = item.getItemId();
      Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), item.getTitle() + " clicked", Snackbar.LENGTH_SHORT);
      Fragment fragment = null;
      switch (id) {
          case R.id.nav_home:
              fragment = HomeFragment.getFragInstance();
              break;
  
          case R.id.nav_news:
  
              fragment = NewsFragment.getFragInstance();
              break;
  
  
          default:
              break;
      }
  
      if (fragment != null) {
          FragmentTransaction transaction = fragmentManager.beginTransaction();
          transaction.addToBackStack(null);
          transaction.replace(R.id.drawer_layout, fragment);
          transaction.commit();
  
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          drawer.closeDrawer(GravityCompat.START);
  
      }
      return true;
  }
  </code></pre>
  
  <p>but the layout for the <code>home</code> layout is also shown in the <code>news</code> layout.
  This is probably happening because of the line:</p>
  
  <pre><code>transaction.replace(R.id.drawer_layout, fragment);
  </code></pre>
  
  <p>Fragments are supposed to be replaced in a <code>FrameLayout</code> and the old Navigation Drawer layout looked like this:</p>
  
  <pre><code>&lt;android.support.v4.widget.DrawerLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  
  &lt;!-- Framelayout to display Fragments --&gt;
  &lt;FrameLayout
      android:id="@+id/frame_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent" /&gt;
  
  &lt;!-- Listview to display slider menu --&gt;
  
  
  &lt;ListView
      android:id="@+id/list_sliderMenu"
      android:layout_width="240dp"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:choiceMode="singleChoice"
      android:divider="@color/white"
      android:dividerHeight="1dp"
      android:listSelector="@drawable/list_selector"
      android:background="@color/list_background"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>But the new one looks like this:</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
    &lt;android.support.v4.widget.DrawerLayout      
     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/drawer_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fitsSystemWindows="true"
     tools:openDrawer="start"&gt;
  
  &lt;include
      layout="@layout/app_bar_base"
      android:layout_width="match_parent"
      android:layout_height="match_parent" /&gt;
  
  &lt;android.support.design.widget.NavigationView
      android:id="@+id/nav_view"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:fitsSystemWindows="true"
      app:headerLayout="@layout/nav_header_base"
      app:menu="@menu/activity_base_drawer" /&gt;
  
      &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>Long story short, how can one modify the new template to be able to switch between Fragments?</p>