title: Android onDetach() not called when I replace fragment
body: <p>I have two fragments (FRAGMENT_1, FRAGMENT_2). FRAGMENT_1 is running a timer and when I open FRAGMENT_2, I want FRAGMENT_1 to keep running it.</p>
  
  <p><strong>FRAGMENT_1 in onCreate():</strong></p>
  
  <pre><code>if (savedInstanceState == null) {
      FRAGMENT_1 fragment1= new FRAGMENT_1();
      fragment1.setRetainInstance(true);
      getSupportFragmentManager()
          .beginTransaction()
          .add(R.id.main_fragment_container, fragment1, FRAGMENT_1_TAG)
          .addToBackStack(null)
          .commit();
  }
  </code></pre>
  
  <p>This is my navigation between fragments:</p>
  
  <pre><code>@SuppressWarnings("StatementWithEmptyBody")
  @Override
  public boolean onNavigationItemSelected(MenuItem item) {
      // Handle navigation view item clicks here.
      int id = item.getItemId();
      Fragment fragment = null;
      String fragTag = "";
      switch (id) {
          case R.id.nav_frag_1:
              //this method returns
              //getSupportFragmentManager().getFragments()
              //.contains(getSupportFragmentManager().findFragmentByTag(tag))
              if (isFragmentCreated(FRAGMENT_1_TAG)) {
                  fragment = getSupportFragmentManager()
                          .findFragmentByTag(FRAGMENT_1_TAG);
              } else {
                  fragment = new FRAGMENT_1();
              }
              fragTag = FRAGMENT_1_TAG;
              break;
  
          case R.id.nav_frag_2:
              fragment = new FRAGMENT_2();
              fragTag = FRAGMENT_2_TAG;
              break;
  
          case R.id.nav_settings:
              Intent intent = new Intent(this, SettingsActivity.class);
              startActivity(intent);
              break;
  
      }
      if (fragment != null &amp;&amp; !fragment.isVisible()) {
          FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                  .beginTransaction()
                  .replace(R.id.main_fragment_container, fragment, fragTag);
          if (fragment.getTag().equals(FRAGMENT_1_TAG)){
              fragmentTransaction.addToBackStack(null);
          }
          fragmentTransaction.commit();
      }
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      drawer.closeDrawer(GravityCompat.START);
      return true;
  }
  </code></pre>
  
  <p>When I navigate between these fragments, FRAGMENT_2 doesn't call <code>onDetach()</code> therefore it is not removed, but if I remove the following piece of code:</p>
  
  <pre><code>if (fragment.getTag().equals(TIMER_FRAGMENT_TAG)){
      fragmentTransaction.addToBackStack(null);
  }
  </code></pre>
  
  <p>then FRAGMENT_2 calls <code>onDetach()</code> and then gets removed. Why is this happening?</p>