title: Fragment in ViewPager not restored after popBackStack
body: <h2>Problem</h2>
  
  <p>A Fragment is not reattached to its hosting ViewPager after returning from another fragment.</p>
  
  <h2>Situation</h2>
  
  <p>One Activity hosting a Fragment whose layout holds a ViewPager (<code>PageListFragment</code> in the example below). The ViewPager is populated by a FragmentStateViewPagerAdapter. The single Fragments hosted inside the pager (<code>PageFragment</code> in the example below) can open sub page lists, containing a new set of pages.</p>
  
  <h2>Behaviour</h2>
  
  <p>All works fine as long as the back button is not pressed. As soon as the user closes one of the sub PageLists the previous List is recreated, but without the Page that was displayed previously. Swiping through the other pages on the parent PageList still works.</p>
  
  <h2>Code</h2>
  
  <p>A sample application can be found on <a href="https://github.com/imhoff/FragmentPager" rel="noreferrer">github</a>: </p>
  
  <h3><a href="https://github.com/imhoff/FragmentPager/blob/master/FragmentPager/src/com/example/fragmentpager/MainActivity.java" rel="noreferrer">Activity</a></h3>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
  private static final String CURRENT_FRAGMENT = MainActivity.class.getCanonicalName() + ".CURRENT_FRAGMENT";
  
  public static final String ARG_PARENTS = "Parents";
  
  public void goInto(String mHostingLevel, String mPosition) {
      Fragment hostingFragment = newHostingFragment(mHostingLevel, mPosition);
      addFragment(hostingFragment);
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      addBaseFragment();
  }
  
  private void addBaseFragment() {
      Fragment hostingFragment = newHostingFragment("", "");
      addFragment(hostingFragment);
  }
  
  private Fragment newHostingFragment(String mHostingLevel, String oldPosition) {
      Fragment hostingFragment = new PageListFragment();
      Bundle args = new Bundle();
      args.putString(ARG_PARENTS, mHostingLevel + oldPosition +" &gt; ");
      hostingFragment.setArguments(args);
      return hostingFragment;
  }
  
  private void addFragment(Fragment hostingFragment) {
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
      transaction.replace(R.id.fragmentSpace, hostingFragment, CURRENT_FRAGMENT);
      transaction.addToBackStack(null);
      transaction.commit();
  }
  
  }
  </code></pre>
  
  <h3><a href="https://github.com/imhoff/FragmentPager/blob/master/FragmentPager/src/com/example/fragmentpager/PageListFragment.java" rel="noreferrer">PageListFragment</a></h3>
  
  <pre><code>public class PageListFragment extends Fragment {
  
  private String mParentString;
  
  public PageListFragment() {
      // Required empty public constructor
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_hosting, container, false);
  }
  
  @Override
  public void onResume() {
      mParentString = getArguments().getString(MainActivity.ARG_PARENTS);
      ViewPager viewPager = (ViewPager) getView().findViewById(R.id.viewPager);
      viewPager.setAdapter(new SimpleFragmentStatePagerAdapter(getFragmentManager(),mParentString));
      super.onResume();
  }
  
  private static class SimpleFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
  
      private String mHostingLevel;
  
      public SimpleFragmentStatePagerAdapter(FragmentManager fm, String hostingLevel) {
          super(fm);
          this.mHostingLevel = hostingLevel;
      }
  
      @Override
      public android.support.v4.app.Fragment getItem(int position) {
          PageFragment pageFragment = new PageFragment();
          Bundle args = new Bundle();
          args.putString(MainActivity.ARG_PARENTS, mHostingLevel);
          args.putInt(PageFragment.ARG_POSITION, position);
          pageFragment.setArguments(args);
          return pageFragment;
      }
  
      @Override
      public int getCount() {
          return 5;
      }
  }
  }
  </code></pre>
  
  <h3><a href="https://github.com/imhoff/FragmentPager/blob/master/FragmentPager/src/com/example/fragmentpager/PageFragment.java" rel="noreferrer">PageFragment</a></h3>
  
  <pre><code>public class PageFragment extends Fragment {
  
  public static final String ARG_POSITION = "Position";
  
  private String mHostingLevel;
  private int mPosition;
  
  public PageFragment() {
      // Required empty public constructor
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View contentView = inflater.inflate(R.layout.fragment_page, container, false);
      setupTextView(contentView);
      setupButton(contentView);
      return contentView;
  }
  
  private void setupTextView(View contentView) {
      mPosition = getArguments().getInt(ARG_POSITION);
      mHostingLevel = getArguments().getString(MainActivity.ARG_PARENTS);
      TextView text = (TextView) contentView.findViewById(R.id.textView);
      text.setText("Parent Fragments " + mHostingLevel + " \n\nCurrent Fragment "+ mPosition);
  }
  
  private void setupButton(View contentView) {
      Button button = (Button) contentView.findViewById(R.id.button);
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              openNewLevel();
          }
      });
  }
  
  protected void openNewLevel() {
      MainActivity activity = (MainActivity) getActivity();
      activity.goInto(mHostingLevel, Integer.toString(mPosition));
  }
  
  }
  </code></pre>