How do I use get a ViewPager to let go of it's fragments when switching orientation?
<p>My application has a layout which differs based on the orientation and device size. On larger devices it is composed of two views visible at the same time. On smaller devices the layout is a ViewPager that lets the user switch between the two panes. This is a typical pattern.</p>

<p>The problem is that the ViewPager appears to stick around, and keeps its fragments. However, the same fragment instance is not kept for TextRenderFragment.  Instead, it appears that the class is created anew every time. Since the fragments are kept inside the ViewPager, I end up having two sets of content views. This causes problems restoring state because the two sets of views don't share a Bundle.</p>

<p>Given that this is such a common pattern, I expected to find a discussion about how to do this properly, but have been unable. I would either like to have the ViewPager let go of its fragments, or find a way to use the same content fragment instances in both the ViewPager and the static layout, depending on orientation.</p>

<p>The construction looks like this:</p>

<pre class="lang-java prettyprint-override"><code>@Override
public View onCreateView(final LayoutInflater inflater,
        final ViewGroup container, final Bundle saved_instance_state) {

    if (saved_instance_state != null) {
        current_book = saved_instance_state.getInt("book");
        current_chapter = saved_instance_state.getInt("chapter");
    }

    // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.fragment_text_render,
            container, false);

    _configure_content_fragments(v);
    return v;
}

private void _configure_content_fragments(final View v) {
    final ViewPager vp = (ViewPager) v.findViewById(R.id.tr_view_pager);
    bl = new DocLoader();

    boolean add_fragments = false;
    final FragmentManager mgr = getFragmentManager();
    primary_content_fragment = (PrimaryContent) mgr
            .findFragmentByTag("primary_content_fragment");
    secondary_content_fragment = (SecondaryContent) mgr
            .findFragmentByTag("secondary_content_fragment");

    if (primary_content_fragment == null) {
        primary_content_fragment = new PrimaryContent();
        secondary_content_fragment = new SecondaryContent();
        primary_content_fragment.setNavLocation(current_book,
                current_chapter);
        add_fragments = true;
    }

    if (add_fragments &amp;&amp; vp == null) {
        final android.support.v4.app.FragmentTransaction tx = getFragmentManager()
                .beginTransaction();
        tx.add(R.id.tr_primary_content_container, primary_content_fragment,
                "primary_content_fragment");
        tx.add(R.id.tr_secondary_content_container,
                secondary_content_fragment, "secondary_content_fragment");
        tx.commit();
    }

}
</code></pre>

<p>The initialization of the ViewPager looks like this:</p>

<pre class="lang-java prettyprint-override"><code>@Override
public void onStart() {
    super.onStart();
    _configure_view_pager(getView());
}
 private void _configure_view_pager(final View v) {
    final ViewPager vp = (ViewPager) v.findViewById(R.id.tr_view_pager);

    if (vp != null) {
        final FragmentManager mgr = getFragmentManager();
        final FragmentStatePagerAdapter adapter = new FragmentStatePagerAdapter(
                mgr) {

            @Override
            public int getCount() {
                return 2;
            }

            @Override
            public Fragment getItem(final int item) {
                switch (item) {
                case 0:
                    return primary_content_fragment;
                case 1:
                    return secondary_content_fragment;
                }

                return null;
            }
        };

        vp.setAdapter(adapter);
    }
}
</code></pre>
