title: ViewPager Fragments are not recreated after FragmentTransaction.replace() followed by back button
body: <p>I am trying to implement saving and restoring state, but I am running into problems when replacing the main <code>Fragment</code> with a <code>PreferenceFragment</code> and then hitting the back button. My main <code>Fragment</code> consists of a <code>ViewPager</code> with a <code>FragmentPagerAdapter</code> with 3 <code>Fragments</code> to swipe through. None of the <code>Fragment.onCreateView()</code> callbacks for my 3 <code>Fragment</code>s are invoked after hitting the back button. I have tried all the solutions I have come over here on SO, but I have not been able to resolve the issue.</p>
  
  <p>Another possibly important thing to note is that the data for my 3 <code>ViewPager</code> <code>Fragment</code>s are stored in separate classes which are instanced and accessible through the <code>Activity</code>. The 3 <code>Fragment</code>s all contain <code>RecyclerView</code>s for listing a fair amount of data. This is done for cleanliness, but also so that these data persist in the <code>Activity</code>. This is <em>probably</em> not an issue, since it works fine when starting the app, but also since the main issue is that my <code>Fragments</code> are not recreated.</p>
  
  <p><strong>Unexpected behavior:</strong></p>
  
  <p>On application creation, everything works fine, but when I replace my main <code>Fragment</code> (containing a <code>ViewPager</code> with a <code>FragmentPagerAdapter</code>) with another one and then pressing the back button, the <code>Fragment</code>s in the <code>ViewPager</code> are not recreated. The <code>onCreateView()</code> of my main <code>Fragment</code> <em>is</em> called.</p>
  
  <p><strong>Questions:</strong></p>
  
  <p>What am I missing? Is there some other callbacks that should be created? Where and how should I recreate the <code>Fragments</code>?</p>
  
  <p><strong>What have I tried:</strong></p>
  
  <ol>
  <li>Changed the <code>FragmentAdapter</code> in use, but I should really use <code>getSupportFragmentManager()</code> as was the solution <a href="https://stackoverflow.com/questions/16600156/fragment-in-viewpager-not-restored-after-popbackstack">here</a>.</li>
  </ol>
  
  <p><strong>EDIT: Use of erroneous <code>FragmentManager</code> actually <em>was</em> the source of my troubles. See my answer below.</strong> </p>
  
  <ol start="2">
  <li>Add
  <code>@Override public int getItemPosition(Object object) {return POSITION_NONE;}</code> to the <code>FragmentPagerAdapter</code>, as suggested <a href="https://stackoverflow.com/questions/16671695/how-to-recreate-all-fragments-in-viewpager">here</a>.</li>
  <li>Change from <code>FragmentPagerAdapter</code> to <code>FragmentStatePagerAdapter</code> which should not matter here, as far as I understand. The internals of <code>FragmentStatePagerAdapter</code> actually keeps the <code>Fragments</code>, but they are not rendered anew.</li>
  <li>Add the main <code>Fragment</code> back using a <code>FragmentTransaction</code> when its <code>onCreateView()</code> is called and in several of the other callbacks of that <code>Fragment</code>, see <code>MMMainFragment</code> below.</li>
  <li>Various other things.</li>
  </ol>
  
  <h3>My MainFragment class with corresponding XML layout</h3>
  
  <pre><code>public class MMMainFragment extends Fragment
  {
      private MMViewPager mMMViewPager = null;
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      {
          // Note that this method IS called when the back button is pressed.
          // I have tried setting the content back to this instance in several places.
  
          View view = inflater.inflate(R.layout.mm_main_fragment, container, false);
  
          // Setup ViewPager.
          mMMViewPager = (MMViewPager) view.findViewById(R.id.mm_pager);
          TabLayout tabLayout = (TabLayout) view.findViewById(R.id.mm_tablayout);
          tabLayout.setupWithViewPager(mMMViewPager);
  
          return view;
      }
  
      public MMViewPager getMMViewPager()
      {
          return mMMViewPager;
      }
  }
  </code></pre>
  
  <h3>mm_main_fragment.xml</h3>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;android.support.design.widget.TabLayout
          android:id="@+id/mm_tablayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:tabMode="scrollable" /&gt;
  
      &lt;com.mycompany.myapp.gui.mmpager.MMViewPager
          android:id="@+id/mm_pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1" /&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Then, in <code>Activity.onCreate()</code> i simply do:</p>
  
  <pre><code>// Insert Main Fragment.
  FragmentManager fragmentManager = getSupportFragmentManager();
  fragmentManager.beginTransaction()
          .replace(R.id.mm_content_frame, new MMMainFragment())
          .commit();
  </code></pre>
  
  <p>where <code>mm_content_frame</code> is a <code>FrameLayout</code> where I replace and remove <code>Fragment</code>s. Then, when a button to view the preferences are pushed, I run the following snippet below in the <code>Activity</code>. I add this <code>Fragment</code> to the backstack to be able to use the back button.</p>
  
  <pre><code>public void showSettingsFragment()
  {
      getSupportActionBar().setTitle(getString(R.string.mm_drawer_settings));
      getSupportFragmentManager().beginTransaction()
              .replace(R.id.mm_content_frame, new MMPreferencesFragment(), FragmentConstants.SETTINGS_FRAGMENT_TAG)
              .addToBackStack(null)
              .commit();
  }
  </code></pre>
  
  <h3>The MMViewPager clas:</h3>
  
  <pre><code>public class MMViewPager extends ViewPager
  {
      private MMActivity mMMActivity;
      private MMPagerAdapter mMMPagerAdapter;
  
      public MMViewPager(Context context, AttributeSet attrs)
      {
          super(context, attrs);
          mMMActivity = (MMActivity) context;
          mMMPagerAdapter = new MMPagerAdapter(mMMActivity.getSupportFragmentManager(), mMMActivity);
  
          this.setOffscreenPageLimit(PagerConstants.OFFSCREEN_PAGE_LIMIT);
          this.setAdapter(mMMMPagerAdapter);
          this.setCurrentItem(PagerConstants.PAGE_FILTER_RECIPES);
      }
  }
  </code></pre>
  
  <h3>The MMPagerAdapter class, currently a <code>FragmentStatePagerAdapter</code>:</h3>
  
  <pre><code>public class MMPagerAdapter extends FragmentStatePagerAdapter
  {
      private MMActivity mMMActivity;
  
      public MMPagerAdapter(FragmentManager fragmentManager, MMActivity mmActivity)
      {
          super(fragmentManager);
          mMMActivity = mmActivity;
      }
  
      @Override
      public Fragment getItem(int position)
      {
          switch (position)
          {
              case PagerConstants.PAGE_FILTER_RECIPES:
                  return new FilteredRecipesFragment();
  
              case PagerConstants.PAGE_SELECTED_RECIPES:
                  return new SelectedRecipesFragment();
  
              case PagerConstants.PAGE_SHOPPING_LIST:
                  return new ShoppingListFragment();
  
              default:
                  return null;
          }
      }
  
      @Override
      public int getCount()
      {
          return PagerConstants.NUMBER_OF_PAGES; // 3
      }
  
      @Override
      public CharSequence getPageTitle(int position)
      {
          switch (position)
          {
              case PagerConstants.PAGE_FILTER_RECIPES:
                  return mMMActivity.getResources().getString(R.string.mm_title_recipe_filter_fragment);
  
              case PagerConstants.PAGE_SELECTED_RECIPES:
                  return mMMActivity.getResources().getString(R.string.mm_title_selected_recipes_fragment);
  
              case PagerConstants.PAGE_SHOPPING_LIST:
                  return mMMActivity.getResources().getString(R.string.mm_title_shopping_list_fragment);
  
              default:
                  return "Tab";
          }
      }
  }
  </code></pre>
  
  <h3>For reference, here is also the main layout of the <code>Activity</code>:</h3>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v4.widget.DrawerLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/mm_drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;!-- Source: http://developer.android.com/training/implementing-navigation/nav-drawer.html --&gt;
      &lt;!-- The main content view --&gt;
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;!-- Source: http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html --&gt;
          &lt;android.support.v7.widget.Toolbar
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/mm_toolbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:minHeight="?attr/actionBarSize"
              android:layout_alignParentTop="true"
              style="@style/MMActionBar"
              app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
              app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /&gt;
  
          &lt;!-- The FrameLayout where I replace Fragments --&gt;
          &lt;FrameLayout
              android:id="@+id/mm_content_frame"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_below="@id/mm_toolbar"/&gt;
  
      &lt;/RelativeLayout&gt;
  
  
      &lt;ListView
          android:id="@+id/mm_drawer_listview"
          android:layout_width="260dp"
          android:layout_height="match_parent"
          android:paddingLeft="16dp"
          android:paddingRight="16dp"
          android:paddingStart="16dp"
          android:layout_gravity="start"
          android:choiceMode="singleChoice"
          android:divider="@android:color/transparent"
          android:dividerHeight="0dp"
          android:background="@color/mm_white" /&gt;
  
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>