title: Difference between add() & replace() with Fragment's lifecycle
body: <p>My program has 6 fragments: Fragment1, Fragment2,....->Fragment6.</p>
  
  <p>I use the add() and replace() statement to switch between the fragment and track their lifecycle.</p>
  
  <p>Fragment1 <strong>add</strong> Fragment2 <strong>add</strong> Fragment3 <strong>add</strong> Fragment4 <strong>add</strong> Fragment5 <strong>replace</strong> Fragment6</p>
  
  <p>The log-cat to shown their lifecycle (i have some printf-points in onCreate, onCreateView, onDestroyView, onDestroy for tracking)</p>
  
  <hr>
  
  <p><strong>Tag</strong> <em><strong></em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__</strong> <em><strong>Text</em></strong></p>
  
  <p>Fragment1_<strong><em>_</em>__<em>_</em>___</strong>_onCreate</p>
  
  <p>Fragment1_<strong><em>_</em>__<em>_</em>___</strong>_onCreateView</p>
  
  <p>Fragment1_<strong><em>_</em>__<em>_</em>___</strong>_add Fragment2</p>
  
  <p>Fragment2_<strong><em>_</em>__<em>_</em>___</strong>_onCreate</p>
  
  <p>Fragment2_<strong><em>_</em>__<em>_</em>___</strong>_onCreateView</p>
  
  <p>Fragment2_<strong><em>_</em>__<em>_</em>___</strong>_add Fragment3</p>
  
  <p>Fragment3_<strong><em>_</em>__<em>_</em>___</strong>_onCreate</p>
  
  <p>Fragment3_<strong><em>_</em>__<em>_</em>___</strong>_onCreateView</p>
  
  <p>Fragment3_<strong><em>_</em>__<em>_</em>___</strong>_add Fragment4</p>
  
  <p>Fragment4_<strong><em>_</em>__<em>_</em>___</strong>_onCreate</p>
  
  <p>Fragment4_<strong><em>_</em>__<em>_</em>___</strong>_onCreateView</p>
  
  <p>Fragment4_<strong><em>_</em>__<em>_</em>___</strong>_add Fragment5</p>
  
  <p>Fragment5_<strong><em>_</em>__<em>_</em>___</strong>_onCreate</p>
  
  <p>Fragment5_<strong><em>_</em>__<em>_</em>___</strong>_onCreateView</p>
  
  <p><em><strong>Fragment5</em></strong> <em><strong></em>__<em>_</em>__<em>_</em>_</strong> <em><strong>replace Fragment6</em></strong></p>
  
  <p><em><strong>Fragment1</em></strong> <em><strong></em>__<em>_</em>__<em>_</em>_</strong> <em><strong>onDestroyView</em></strong></p>
  
  <p><em><strong>Fragment3</em></strong> <em><strong></em>__<em>_</em>__<em>_</em>_</strong> <em><strong>onDestroyView</em></strong></p>
  
  <p><em><strong>Fragment5</em></strong> <em><strong></em>__<em>_</em>__<em>_</em>_</strong> <em><strong>onDestroyView</em></strong></p>
  
  <p>Fragment6_<strong><em>_</em>__<em>_</em>___</strong>_onCreate</p>
  
  <p>Fragment6_<strong><em>_</em>__<em>_</em>___</strong>_onCreateView</p>
  
  <hr>
  
  <p>My questions:</p>
  
  <p><strong>Why</strong> after the Fragment5 is <strong>replaced</strong> by Fragment6, the Fragment1 &amp; 3 &amp;5 are destroyed their view ?.</p>
  
  <p><strong>What</strong> is happending with Fragment2 &amp; 4 ?</p>
  
  <p><strong>Why</strong> Fragment2 &amp; 4 are not destroyed their view as Fragment1 &amp; 3 &amp;5 ?</p>
  
  <p>Please help me to understand fully about fragment's lifecycle when call the add() and replace() method.</p>
  
  <hr>
  
  <p>Update my addFragment and replaceFragment method:</p>
  
  <pre><code>public void addFragment(Fragment fromFragment, Fragment toFragment) {
      FragmentManager manager = getFragmentManager();
      FragmentTransaction transaction = manager.beginTransaction();
      transaction.add(R.id.container,toFragment, toFragment.getClass().getName());
      transaction.hide(fromFragment);
      transaction.addToBackStack(toFragment.getClass().getName());
      transaction.commit();
  }
  
  public void replaceFragment(Fragment fromFragment, Fragment toFragment) {
      FragmentManager manager = getFragmentManager();
      FragmentTransaction transaction = manager.beginTransaction();
      transaction.replace(R.id.container,toFragment, toFragment.getClass().getName());
      transaction.hide(fromFragment);
      transaction.addToBackStack(toFragment.getClass().getName());
      transaction.commit();
  }
  </code></pre>