title: Strange behavior of fragmentTransaction.replace() method
body: <p>I have just create a demo to understand the replace method of FragmentTransaction and I am not getting result as per <a href="https://developer.android.com/reference/android/app/FragmentTransaction.html#replace(int,%20android.app.Fragment,%20java.lang.String)" rel="nofollow noreferrer">Developer Guide</a>.</p>
  
  <p><strong>Activity and Fragment</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private int count;
      private Fragment prevFragment;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          final Button button = (Button) findViewById(R.id.activity_main_bv_add);
  
  
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  count++;
  
                  final FragmentTransaction transaction = getFragmentManager().beginTransaction();
                  final TestFragment fragment = new TestFragment(count);
  
                  if (count == 1) {
                      transaction.add(R.id.activity_main_rl_container, fragment, fragment.getClass().getSimpleName());
  
                  } else if (count == 5) {
                      transaction.replace(R.id.activity_main_rl_container, fragment, fragment.getClass().getSimpleName());
                      transaction.addToBackStack(null);
                      if (prevFragment != null) {
                          transaction.hide(prevFragment);
                      }
                  } else {
                      transaction.add(R.id.activity_main_rl_container, fragment, fragment.getClass().getSimpleName());
                      transaction.addToBackStack(null);
                      if (prevFragment != null) {
                          transaction.hide(prevFragment);
                      }
                  }
                  prevFragment = fragment;
  
                  transaction.commit();
  
  
              }
          });
      }
  
      public class TestFragment extends Fragment {
          private final String TAG = this.getClass().getSimpleName();
          private int number;
  
  
          public TestFragment(final int number) {
              this.number = number;
          }
  
          public TestFragment() {
          }
  
          @Nullable
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              Log.e(TAG, "onCreateView : " + number);
  
              final View view = inflater.inflate(R.layout.row_button, null);
  
              final Button button = (Button) view.findViewById(R.id.button);
              button.setText("" + number);
  
              return view;
          }
  
          @Override
          public void onDestroyView() {
              super.onDestroyView();
              Log.e(TAG, "onDestroyView : " + number);
          }
      }
  }
  </code></pre>
  
  <p><strong>Fragment xml</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary"
                android:orientation="vertical"&gt;
  
      &lt;Button
          android:id="@+id/button"
          android:layout_width="match_parent"
          android:layout_height="40dp"
          android:focusable="false"
          android:focusableInTouchMode="false"
          /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Activity xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout android:id="@+id/activity_currency_select"
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:descendantFocusability="blocksDescendants"
      &gt;
  
      &lt;Button
          android:id="@+id/activity_main_bv_add"
          android:layout_width="match_parent"
          android:layout_height="40dp"
          android:text="Add More"/&gt;
  
      &lt;RelativeLayout
          android:id="@+id/activity_main_rl_container"
          android:layout_width="match_parent"
          android:layout_height="300dp"
          android:layout_below="@id/activity_main_bv_add"
          android:layout_marginTop="20dp"
          android:background="@color/colorAccent"&gt;&lt;/RelativeLayout&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>FragmentTransaction replace method</strong> : 
  As per Android document,
  Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.</p>
  
  <p><strong>Assume Case 1</strong> : Using replace method will remove current(last and latest) fragment and add new one in container view.</p>
  
  <p><strong>Assume Case 2</strong> : Using replace method will remove add the fragments that are added into same container view.</p>
  
  <p>But as per above code non of the above case seems right.</p>
  
  <p>Let's click on button as per shown code.
  On every button click till count == 4, new object of Fragment will be added to container.</p>
  
  <p>When we click on button, for the count == 5, here replace method is used at that time new object of Fragment will be added to container and onDestroyView method is called for the 1st and 3rd added fragments</p>
  
  <p><strong>Question 1 :</strong> If replace will remove all the fragments added in same container then why onDestroyView is not called for 2nd and 4th added fragments?</p>
  
  <p><strong>Question 2 :</strong> If replace method removes last added fragment in same container then why it does not destroy 4th fragment but 1st and 3rd instead?</p>
  
  <p>As per my understanding the behaviour dose not follow the document showing on <a href="https://developer.android.com/reference/android/app/FragmentTransaction.html#replace(int,%20android.app.Fragment,%20java.lang.String)" rel="nofollow noreferrer">Developer site</a>. </p>
  
  <p>Correct me if I'm wrong.</p>