title: What is the lifecycle of a fragment when replace() is called?
body: <p>I have a number of fragments which are dynamically added using the following code:</p>
  
  <pre><code>private class DrawerItemClickListener implements ListView.OnItemClickListener {
      @Override
      public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
          selectItem(position);
      }
  }
  
  private void selectItem(int position) {
      // update the main content by replacing fragments
      Fragment fragment = null;
      if(position == 0){
          fragment = new FirstFragment();
      }
      else if(position == 1){
          fragment = new SecondFragment();
      }
      else if(position == 2){
          fragment = new ThirdFragment();
      }
  
      FragmentManager fragmentManager = getSupportFragmentManager();
      fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
  
      // update selected item and title, then close the drawer
      mDrawerList.setItemChecked(position, true);
      setTitle(mCalculatorTitles[position]);
      mDrawerLayout.closeDrawer(mDrawerList);
  }
  </code></pre>
  
  <p>In one of my fragments I have a timer and I've just discovered that when the fragment is replaced the timer in the old fragment is still running. Where in my fragment's lifecycle should I kill the timer?</p>
  
  <p>EDIT:</p>
  
  <p>Okay so I added a timer.cancel() in the fragment's onStop() method but onStop() also gets called when I load the preferences from a button on the action bar. This is not the desired effect. Any other ideas?</p>