title: ViewPager with Nested Fragments?
body: <h2>My problem</h2>
  
  <p>According to the Google's docs:</p>
  
  <blockquote>
    <p>You can now embed fragments inside fragments. This is useful for a
    variety of situations in which you want to place dynamic and re-usable
    UI components into a UI component that is itself dynamic and
    re-usable. For example, <strong>if you use ViewPager to create fragments that</strong>
     <strong>swipe left and right and consume a majority of the screen space, you</strong>
     <strong>can now insert fragments into each fragment page.</strong> To nest a fragment,
    simply call getChildFragmentManager() on the Fragment in which you
    want to add a fragment. This returns a FragmentManager that you can
    use like you normally do from the top-level activity to create
    fragment transactions. For example, here’s some code that adds a
    fragment from within an existing Fragment class:</p>
  </blockquote>
  
  <pre><code>Fragment videoFragment = new VideoPlayerFragment();
  FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
  transaction.add(R.id.video_fragment, videoFragment).commit();
  </code></pre>
  
  <p>So I created my own <code>PageFragment</code>. And my <code>PageFragmentAdapter</code>consist of 3 pages of <code>PageFragment</code>.
  I also have six fragments: <code>Fragment1</code>, <code>Fragment2</code>, <code>Fragment3</code>, <code>FragmentA</code>, <code>FragmentB</code>, <code>FragmentC</code>.
  In my <code>MainActivity</code> I initialize my six fragments, my <code>ViewPager</code> and its <code>PageFragmentAdapter</code> (which initialize three <code>PageFragment</code> for itself).</p>
  
  <p>Then I use this method to attach my <code>Fragment1</code>, <code>Fragment2</code>, <code>Fragment3</code> to each page (to each <code>PageFragment</code>) of <code>PageFragmentAdapter</code>:</p>
  
  <pre><code>PageFragmentAdapter tPageFragmentAdapter = new PageFragmentAdapter(getSupportFragmentManager());
  tPageFragmentAdapter.setFirstPage(tFragment1);
  tPageFragmentAdapter.setSecondPage(tFragment2);
  tPageFragmentAdapter.setThirdPage(tFragment3);
  </code></pre>
  
  <p>I can recive (via methods in <code>PageFragmentAdapter</code>) Fragments and make "nested fragment" into my <code>PageFragment</code> (of course in <code>onCreate()</code>) like:       </p>
  
  <pre><code>getChildFragmentManager().beginTransaction()
  .add(R.id.framelayout_history, mFragment)
  .addToBackStack(null)
  .commit();
  </code></pre>
  
  <p><em>Note: I use <code>PageFragment.setNestedFragment()</code> to set <code>mFragment</code> in it.</em></p>
  
  <p>And it works great!
  It will help me (but this story is not about it) to simply replace fragments in ViewPager like:</p>
  
  <pre><code>tPageFragmentAdapter.replaceFirstPage(tFragmentA);
  </code></pre>
  
  <p>But I have one huge problem.
  Again Google's docs:</p>
  
  <blockquote>
    <p>Implementation of PagerAdapter that represents each page as a Fragment
    that is persistently kept in the fragment manager as long as the user
    can return to the page. This version of the pager is best for use when
    there are a handful of typically more static fragments to be paged
    through, such as a set of tabs. <strong>The fragment of each page the user</strong>
     <strong>visits will be kept in memory, though its view hierarchy may be</strong>
     <strong>destroyed when not visible.</strong> This can result in using a significant
    amount of memory since fragment instances can hold on to an arbitrary
    amount of state.</p>
  </blockquote>
  
  <p>As you can see <code>PageFragmentAdapter</code> can destroy each my <code>PageFragment</code> and of course its nested fragment. And when it will be recreated it will have a NULL instead <code>mFragment</code> value. 
  I am trying to save it via <code>getChildFragmentManager().putFragment()</code> and then get it by <code>getChildFragmentManager().getFragment()</code>.  But it doesn't worked.</p>
  
  <h2>Question</h2>
  
  <p>So my question is: <strong>How to save my nested fragment in my parent fragment? Is it possible?</strong></p>
  
  <p>I would greatly appreciate for your help. Alex. P.S. Really sorry for my English:)</p>
  
  <h2>EDIT</h2>
  
  <p>Thx @AndroidBegin.com for the answer! </p>
  
  <blockquote>
    <p>This seems to be a bug in the newly added support for nested
    fragments. Basically, the child FragmentManager ends up with a broken
    internal state when it is detached from the activity. A short-term
    workaround that fixed it for me is to add the following to onDetach()
    of every Fragment which you call getChildFragmentManager() on:</p>
  </blockquote>
  
  <pre><code>@Override
  public void onDetach() {
      super.onDetach();
  
      try {
          Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
          childFragmentManager.setAccessible(true);
          childFragmentManager.set(this, null);
  
      } catch (NoSuchFieldException e) {
          throw new RuntimeException(e);
      } catch (IllegalAccessException e) {
          throw new RuntimeException(e);
      }
  }
  </code></pre>