title: Programmatically removing a ViewPager, when are (or how can I ensure) contained Fragments are destroyed?
body: <p>Having searched regarding this issue beforehand, I can find many discussions regarding dynamically adding and removing selected <code>Fragment</code>s from a <code>ViewPager</code>. What I'm actually concerned about here however is how I can programmatically remove an entire <code>ViewPager</code> 'cleanly' from its containing <code>ViewGroup</code>, when that <code>ViewPager</code> has been used to display <code>Fragment</code>s via a <code>FragmentPagerAdapter</code>, and ensure that the contained <code>Fragment</code>s are destroyed properly.</p>
  
  <p>To expand on the question a bit more, I have a landscape two-pane layout where a selection is made from a list within a <code>Fragment</code> on the left-hand-side, and chosen content is then placed on the right within a <code>FrameLayout</code>. The key thing is that the content may or may not be paginated. Therefore, the content must either be displayed in a <code>ViewPager</code> format, or if it is not paginated then it shall be represented by a single <code>Fragment</code> directly. </p>
  
  <p>To show a single <code>Fragment</code>, I simply perform a <code>FragmentTransaction</code> as you normally would in order to place the <code>Fragment</code> into the <code>FrameLayout</code> container. If on the other hand it's paginated content to be shown, then instead I create a <code>ViewPager</code> and add it as a child of the <code>FrameLayout</code>. </p>
  
  <p>When I need to change the content, then if the previous content was a stand-alone <code>Fragment</code> then I can simply remove it via <code>FragmentTransaction</code> <code>.remove()</code>. When I do this, the <code>Fragment</code> goes through the <code>onPause()</code> ... <code>onDestroy()</code> cycle as expected. If the previous content was a <code>ViewPager</code> then I remove it from the <code>FrameLayout</code> using <code>.removeAllViews()</code>. Here I come to the problem: I don't see any of the <code>onPause()</code> ... <code>onDestroy()</code> methods being called in any of the <code>Fragment</code>s that were held within that <code>ViewPager</code> via the <code>FragmentPagerAdapter</code>. </p>
  
  <p>From a user point of view, the application works fine. After several rounds of <code>ViewPager</code> being removed, I can see the GC reclaiming memory. However, I don't like the fact that those <code>Fragment</code>s' end of life methods aren't called as I can't do any cleanup within them, and it just doesn't seem 'right'.</p>
  
  <p>Is there a method I can hook into in order to remove the <code>ViewPager</code>'s <code>Fragment</code>s when the <code>ViewPager</code> is detached from its parent, perhaps? In other words, when I know that the <code>ViewGroup</code> is no longer in used, I would perform <code>FragmentTransaction</code>s somewhere (perhaps in the <code>FragmentPagerAdapter</code>) to remove those <code>Fragment</code>s.</p>
  
  <p>Alternatively, I realise that I could just keep the <code>ViewPager</code> on the right permanently, and dynamically swap the <code>Fragment</code>s within it. Of course it simply would not matter that at certain times it would only hold one page. If this would be a better way to go then I shall refactor my code to do this, but I would appreciate opinions.</p>