title: Handling fragment back stack with Navigation Drawer
body: <p>I am implementing navigation drawer and works well. So i am calling fragment on navigation drawer click and it is also working and further more i am calling another fragment from Home page fragment and maintain the back stack for every fragment but the problem is back press from the child fragment i can't go to Home page  fragment and just exited from application. I don't want this. What i want Click on</p>
  
  <pre><code>Navigation Drawer-&gt;HomePageFragment-&gt;AnotherChild Fragment(On List Item click of HomePageFragment)
  </code></pre>
  
  <p>but on back pressed without going to <code>Homepage</code> fragment its directly exit with application. Here is my code: <code>(In Fragment Activity with Navigation Drawer)</code></p>
  
  <pre><code>class SlideitemListener implements ListView.OnItemClickListener {
      @Override
      public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
              long id) {
  
          updateDisplay(position);
      }
  
  }
  
  private void updateDisplay(int position) {
      Fragment fragment = null;
      switch (position) {
      case 0:
          fragment = new ScheduleFragment();
          break;
      case 1:
          fragment = new Result_Fragment();
          break;
      case 2:
          fragment = new Live_Match_Fragment();
          break;
      case 3:
          // fragment = new Live_Match_Fragment();
          break;
  
      case 4:
          fragment = new Team_Fragment();
          break;
      default:
          break;
      }
  
      if (fragment != null) {
  
          fragmentManager = getFragmentManager();
          fragmentManager.popBackStackImmediate("0", 0);
          int count = fragmentManager.getBackStackEntryCount();
          FragmentTransaction fragmentTransaction = fragmentManager
                  .beginTransaction();
          fragmentTransaction.replace(R.id.frame_container, fragment)
                  .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                  .addToBackStack(String.valueOf(count)).commit();
          Log.e("Count in Activiy", ""+count);
  
          // update selected item and title, then close the drawer
          setTitle(menutitles[position]);
          mDrawerLayout.closeDrawer(mDrawerList);
      } else {
          // error in creating fragment
          Log.e("MainActivity", "Error in creating fragment");
      }
  
  }
  </code></pre>
  
  <p>Now in <code>BackPressed()</code> in <code>FragmentActivity</code>.</p>
  
  <pre><code>@Override
  public void onBackPressed() {
      if (fragmentManager.getBackStackEntryCount() &lt;= 1) {
          finish();
  
          return;
      }
      super.onBackPressed();
  }
  </code></pre>
  
  <p>Now calling another child fragment from <code>HomePage</code> fragment on <code>Listview</code> item click.</p>
  
  <pre><code> team_lv.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
                  long arg3) {
              // TODO Auto-generated method stub
              TeamDetailFragment myDetailFragment = new TeamDetailFragment();
              FragmentManager fragmentManager = getFragmentManager();
              int count = fragmentManager.getBackStackEntryCount();
              Log.e("Count in Fragment", "" + count);
              FragmentTransaction fragmentTransaction = fragmentManager
                      .beginTransaction();
              fragmentTransaction
                      .replace(R.id.frame_container, myDetailFragment)
                      .setTransition(
                              FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                      .addToBackStack(String.valueOf(count)).commit();
  
          }
      });
  </code></pre>
  
  <p>So anybody knows then help me. Help will be appreciate. </p>