title: After replacing Fragment, Activity still detects Fragment is visible
body: <p>I have one <code>Activity</code> that hosts many <code>Fragments</code>. In <code>Fragment A</code>, I am adding a dialog when the user presses the back button to ask if they are sure if they want to leave this fragment. I added this code in the Activity's <code>onBackPressed</code> to control this:</p>
  
  <pre><code>@Override
  public void onBackPressed() {
      FragmentA fragmentA = (FragmentA) getSupportFragmentManager().findFragmentByTag("fragmentA");
  
      if (fragmentA != null &amp;&amp; fragmentA.isVisible()) {
          fragmentA.showExitDialog();
          return;
      }
      else { super.onBackPressed(); }
  }
  </code></pre>
  
  <p>And the dialog works perfectly in Fragment A, but when it leaves Fragment A and goes to Fragment B (same activity) with this code:</p>
  
  <pre><code>getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, 
                  FragmentB.newInstance(), "fragmentB").commit();
  </code></pre>
  
  <p>And I press the back button in Fragment B, it shows the same exit dialog again.</p>
  
  <p>Thus, from my code, it is saying that FragmentA is not null and still visible even after I did the <code>Replace</code> function. </p>
  
  <p><strong>Why is Fragment A still visible when I replaced it with Fragment B?</strong></p>