title: Having trouble with onNavigationItemSelected and beginTransaction()
body: <p>I am creating a small app that will allow the user to view different categories of books for college. </p>
  
  <p>So far in the activity main drawer I have a few different categories that should load a layout with a listView and populate this list from an arrayAdapter when selected. I have it set up and semi-working on one of my layouts, but the problem arises when I change to a different category from the navigation drawer, the listView is still displayed on the screen and I can't seem to figure out how to get it to stop.</p>
  
  <p>I thought that I would need to use endTransaction() but I'm not really sure yet if I'm using the right thing.</p>
  
  <p>I am a beginner to Java and Android Studio, so I hope that I am making sense in what I am asking.</p>
  
  <p>If more code or information is needed, I will be happy to provide.</p>
  
  <pre><code>import android.os.Bundle;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentTransaction;
  import android.view.View;
  import android.support.design.widget.NavigationView;
  import android.support.v4.view.GravityCompat;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.ActionBarDrawerToggle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.view.ViewGroup;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.Fragment;
  
  import java.util.ArrayList;
  import java.util.List;
  
  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);
  
      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);
  
      FragmentManager fm = getSupportFragmentManager();
      fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
  
      //loads the home screen when app launches
      displaySelectedScreen(R.id.nav_home);
  
  }
  
  @Override
  public void onBackPressed() {
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      if (drawer.isDrawerOpen(GravityCompat.START)) {
          drawer.closeDrawer(GravityCompat.START);
      } else {
          super.onBackPressed();
      }
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @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.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  private void displaySelectedScreen(int id) {
  
      Fragment fragment = null;
  
      //loads each layout inflator depending on what menu item has been clicked
      switch(id) {
          case R.id.nav_home:
              fragment = new Home();
              break;
          case R.id.nav_book:
              fragment = new All();
              break;
          case R.id.nav_sci_fi:
              fragment = new SciFi();
              break;
          case R.id.nav_fantasy:
              fragment = new Fantasy();
              break;
          case R.id.nav_dystopian:
              fragment = new Dystopian();
              break;
          case R.id.nav_about:
              fragment = new About();
              break;
          case R.id.nav_contact:
              fragment = new ContactUs();
              break;
      }
  
      if(fragment != null) {
  
          FragmentTransaction ft = getSupportFragmentManager(). beginTransaction();
          ft.replace(R.id.content_main, fragment);
          ft.commit();
      }
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      drawer.closeDrawer(GravityCompat.START);
  }
  
  @SuppressWarnings("StatementWithEmptyBody")
  @Override
  public boolean onNavigationItemSelected(MenuItem item) {
      // Handle navigation view item clicks here.
  
      FragmentManager fm=getSupportFragmentManager();
  
      int id = item.getItemId();
  
      if (id == R.id.nav_book) {
          // Handle the camera action
      } else if (id == R.id.nav_sci_fi) {
          fm .beginTransaction().replace(R.id.content_frame, new BookFragment()).commit(); setTitle("Sci-Fi");
          //TODO: endTransaction 
  
      } else if (id == R.id.nav_fantasy) {
  
      } else if (id == R.id.nav_dystopian) {
  
      } else if (id == R.id.nav_about) {
      }
  
      displaySelectedScreen(id);
  
      return true;
  }
  }
  </code></pre>