title: Understanding Fragment's lifeCycle methods calls during fragment transaction
body: <p>I created a demo to understand which all fragment lifecycle's methods are called during different cases of fragment transaction.While most of the calls are as per expectation few things I am still confused which I have written in Bold.</p>
  
  <p>Suppose two fragment A and B are there and we are performing transaction between them</p>
  
  <p>Case 1</p>
  
  <p><strong>When Fragment B is added to Fragment A</strong></p>
  
  <pre><code>getActivity().getSupportFragmentManager().beginTransaction().add(R.id.container, fragementB).addToBackStack(null).commit();
  </code></pre>
  
  <p>Fragment B</p>
  
  <blockquote>
    <p>onAttach</p>
    
    <p>onCreate</p>
    
    <p>onCreateView</p>
    
    <p>onActivityCreated</p>
    
    <p>onStart</p>
    
    <p>onResume</p>
  </blockquote>
  
  <p><strong>No lifecycle  methods of Fragment A is being called.</strong></p>
  
  <p><strong>What i expected was?</strong></p>
  
  <p><strong>onStop method of Fragment A is called since Fragment A is not visible</strong></p>
  
  <p>As per documentation-</p>
  
  <blockquote>
    <p>Stopped- The fragment is not visible. Either the host activity has been
    stopped or the fragment has been removed from the activity but added
    to the back stack. A stopped fragment is still alive (all state and
    member information is retained by the system). However, it is no
    longer visible to the user and will be killed if the activity is
    killed.</p>
  </blockquote>
  
  <p><strong>Does this mean that no method of current fragment is called when new fragment is added in same activity?</strong></p>
  
  <p>Then using <code>popBackStack()</code> in Fragment B</p>
  
  <p>Fragment B</p>
  
  <blockquote>
    <p>onPause</p>
    
    <p>onStop</p>
    
    <p>onDestroyView</p>
    
    <p>onDestroy</p>
    
    <p>onDetach</p>
  </blockquote>
  
  <p><strong>No lifecycle  methods of Fragment A is being called</strong></p>
  
  <p><strong>What i expected was?</strong></p>
  
  <p><strong>onStart method of Fragment A is called since Fragment A is  visible now</strong></p>
  
  <p>Case 2</p>
  
  <p><strong>When Fragment B replaces Fragment A</strong></p>
  
  <pre><code>getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, fragementB).commit();
  </code></pre>
  
  <p>Fragment B</p>
  
  <blockquote>
    <p>onAttach</p>
    
    <p>onCreate</p>
    
    <p>onCreateView</p>
    
    <p>onActivityCreated</p>
    
    <p>onStart</p>
    
    <p>onResume</p>
  </blockquote>
  
  <p>Fragment A</p>
  
  <blockquote>
    <p>onPause</p>
    
    <p>onStop</p>
    
    <p>onDestroyView</p>
    
    <p>onDestroy</p>
    
    <p>onDetach</p>
  </blockquote>
  
  <p>Everything was as per expectation</p>
  
  <p>Case 3</p>
  
  <p><strong>When Fragment B replaces Fragment A keeping it in backstack</strong></p>
  
  <pre><code> getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, fragementB).addToBackStack("tag").commit();
  </code></pre>
  
  <p>Fragment B</p>
  
  <blockquote>
    <p>onAttach</p>
    
    <p>onCreate</p>
    
    <p>onCreateView</p>
    
    <p>onActivityCreated</p>
    
    <p>onStart</p>
    
    <p>onResume</p>
  </blockquote>
  
  <p>Fragment A</p>
  
  <blockquote>
    <p>onPause</p>
    
    <p>onStop</p>
    
    <p>onDestroyView</p>
  </blockquote>
  
  <p><strong>onDestroy and onDetach method of Fragment A is NOT called.Why its not called?Bcoz as per documentation method <code>replace</code> removes any fragments that are already in the container and add your new one to the same container</strong> </p>
  
  <p>Then using <code>popBackStack()</code> in Fragment B</p>
  
  <p>Fragment A</p>
  
  <blockquote>
    <p>onCreateView</p>
    
    <p>onActivityCreated</p>
    
    <p>onStart</p>
    
    <p>onResume</p>
  </blockquote>
  
  <p>Fragment B</p>
  
  <blockquote>
    <p>onPause</p>
    
    <p>onStop</p>
    
    <p>onDestroyView</p>
    
    <p>onDestroy</p>
    
    <p>onDetach</p>
  </blockquote>