title: Navigation Drawer fragments recreated on click. Need to reuse instance of fragment
body: <p>So I use the sliding menu with a few fragments in my application. The issue is that the every time I click on a new fragment in the navigation drawer it appears to completely destroy the old fragment. Either that or the new fragment forces itself to be recreated whenever I click it. Basically I want to know how I can reuse the same instance of the fragment I had whenever I click it. I've looked all over stackoverflow and can't seem to find an answer that works.</p>
  
  <p>Here is my main activity class</p>
  
  <pre><code>public class MainActivity extends Activity {
      private DrawerLayout mDrawerLayout;
      private ListView mDrawerList;
      private ActionBarDrawerToggle mDrawerToggle;
  
  
  
      // nav drawer title
      private CharSequence mDrawerTitle;
  
      // used to store app title
      private CharSequence mTitle;
  
      // slide menu items
      private String[] navMenuTitles;
      private TypedArray navMenuIcons;
  
      private ArrayList&lt;NavDrawerItem&gt; navDrawerItems;
      private NavDrawerListAdapter adapter;
      private Fragment mContent;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          mTitle = mDrawerTitle = getTitle();
  
  
  
  
  
          // load slide menu items
          navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
  
          // nav drawer icons from resources
          navMenuIcons = getResources()
                  .obtainTypedArray(R.array.nav_drawer_icons);
  
          mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
          mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
  
          navDrawerItems = new ArrayList&lt;NavDrawerItem&gt;();
  
          // adding nav drawer items to array
          // Home
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
          // Find People
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
          // Photos
          //navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
          // Communities, Will add a counter here
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
  
  
  
          // Recycle the typed array
          navMenuIcons.recycle();
  
          mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
  
          // setting the nav drawer list adapter
          adapter = new NavDrawerListAdapter(getApplicationContext(),
                  navDrawerItems);
          mDrawerList.setAdapter(adapter);
  
          // enabling action bar app icon and behaving it as toggle button
          getActionBar().setDisplayHomeAsUpEnabled(true);
          getActionBar().setHomeButtonEnabled(true);
  
          mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                  R.drawable.ic_drawer, //nav menu toggle icon
                  R.string.app_name, // nav drawer open - description for accessibility
                  R.string.app_name // nav drawer close - description for accessibility
          ) {
              public void onDrawerClosed(View view) {
                  getActionBar().setTitle(mTitle);
                  // calling onPrepareOptionsMenu() to show action bar icons
                  invalidateOptionsMenu();
              }
  
              public void onDrawerOpened(View drawerView) {
                  getActionBar().setTitle(mDrawerTitle);
                  // calling onPrepareOptionsMenu() to hide action bar icons
                  invalidateOptionsMenu();
              }
          };
          mDrawerLayout.setDrawerListener(mDrawerToggle);
  
          if (savedInstanceState == null) {
              // on first time display view for first nav item
              displayView(0);
          }
      }
  
  
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
  
  
  
  
  
      }
  
  
  /**
       * Slide menu item click listener
       * */
      private class SlideMenuClickListener implements
              ListView.OnItemClickListener {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
                  long id) {
  
  
              // display view for selected nav drawer item
              displayView(position);
  
  
  
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch(item.getItemId()) {
          case R.id.action_about:
              Intent intent = new Intent(this, HelpActivity.class);
              this.startActivity(intent);
              break;
          case R.id.action_settings:
              onLogout();
  
              break;
          default:
  
              if (mDrawerToggle.onOptionsItemSelected(item))
              {
                  return true;
              }
              return super.onOptionsItemSelected(item);
          }
  
          return true;
      }
  
  
      protected void onLogout() {
              try {
                  // clearing app data
                  Runtime runtime = Runtime.getRuntime();
                  runtime.exec("pm clear com.nsi.StudentConnection");
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
  
  
  
      /* *
       * Called when invalidateOptionsMenu() is triggered
       */
      @Override
      public boolean onPrepareOptionsMenu(Menu menu) {
          // if nav drawer is opened, hide the action items
          boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
          menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
          return super.onPrepareOptionsMenu(menu);
      }
  
      /**
       * Diplaying fragment view for selected nav drawer list item
       * */
      private void displayView(int position) {
          // update the main content by replacing fragments
          Fragment fragment = null;
          ListFragment lf = null;
          String TAG;
  
          switch (position) {
  
  
  
  
  
  
          case 0:
  
              fragment = new GradeFragment();
              TAG = "grade";
              break;
  
          case 1:
              lf = new IrvingFragment();
              TAG = "irving";
              break;
  
  
  
              case 2:
              fragment = new HomeworkFragment();
              TAG = "homework";
              break;
  
          //case 2:
              //fragment = new ScheduleFragment();
              //break;
  
  
  
  
          default:
              fragment = new GradeFragment();
              TAG = "grade";
              break;
          }
  
  
          if (fragment != null) {
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction()
                      .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                      .replace(R.id.frame_container, fragment, TAG)
  
  
                      .commit();
  
  
  
  
              // update selected item and title, then close the drawer
              mDrawerList.setItemChecked(position, true);
              mDrawerList.setSelection(position);
              setTitle(navMenuTitles[position]);
              mDrawerLayout.closeDrawer(mDrawerList);
          } else {
              // error in creating fragment
              Log.e("MainActivity", "Error in creating fragment");
          }
  
          if (lf != null) {
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction()
                      .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                      .replace(R.id.frame_container, lf)
  
  
                      .commit();
  
  
  
  
              // update selected item and title, then close the drawer
              mDrawerList.setItemChecked(position, true);
              mDrawerList.setSelection(position);
              setTitle(navMenuTitles[position]);
              mDrawerLayout.closeDrawer(mDrawerList);
          } else {
              // error in creating fragment
              Log.e("MainActivity", "Error in creating fragment");
          }
      }
  
  
  
      @Override
      public void setTitle(CharSequence title) {
          mTitle = title;
          getActionBar().setTitle(mTitle);
      }
  
      /**
       * When using the ActionBarDrawerToggle, you must call it during
       * onPostCreate() and onConfigurationChanged()...
       */
  
      @Override
      protected void onPostCreate(Bundle savedInstanceState) {
          super.onPostCreate(savedInstanceState);
          // Sync the toggle state after onRestoreInstanceState has occurred.
          mDrawerToggle.syncState();
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
          // Pass any configuration change to the drawer toggles
          mDrawerToggle.onConfigurationChanged(newConfig);
      }
  </code></pre>
  
  <p>and here is one of my Fragment classes </p>
  
  <pre><code>public class HomeworkFragment extends Fragment {
  
  
  
  public HomeworkFragment(){}
  protected CheckBox checkbox;
  public static final String PREFS_NAME = "MyPrefsFile";
  
  final String myTag = "DocsUpload";
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      super.onCreate(savedInstanceState); 
      setRetainInstance(true);
  
      View rootView = inflater.inflate(R.layout.fragment_homework, container, false);
  
      SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
         boolean dialogShown = settings.getBoolean("dialogShown", false);
  
       if (dialogShown != true) {
      Dialog dialog = createDialog();
      dialog.show();
      SharedPreferences.Editor editor = settings.edit();
       editor.putBoolean("dialogShown", true);
       editor.commit();
  
       }
  
      return rootView;
  
  
  }
  
  
   public Dialog createDialog()
   {
  
  
  
  
  
  
          // Use the Builder class for convenient dialog construction
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          builder.setTitle(R.string.rebuild_form_title)
                 .setMessage(R.string.rebuild_instructions)
  
                 .setPositiveButton(R.string.dialog, new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                         // User cancelled the dialog
  
                     }
  
                 });
  
  
          // Create the AlertDialog object and return it
  
  
  
       return builder.create();
   }
  
  
  
  
  
      private void doNotShowAgain() {
          // persist shared preference to prevent dialog from showing again.
          Log.d("MainActivity", "TODO: Persist shared preferences.");
  
          SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
  
  
  
      }
  </code></pre>