title: Saving Fragment state in ViewPager
body: <p>I tried out <a href="https://android.googlesource.com/platform/development/+/master/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java" rel="noreferrer">the sample code from the API</a> and it didn't really work so I implemented my own:</p>
  
  <p><strong>FragmentPagerSupport</strong></p>
  
  <pre><code>public class FragmentPagerSupport extends FragmentActivity {
  
  static final int NUM_ITEMS = 10;
  MyAdapter mAdapter;
  ViewPager mPager;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      mAdapter = new MyAdapter(getSupportFragmentManager());
      Log.i("Pager", "mAdapter = " + mAdapter.toString());
  
      mPager = (ViewPager)findViewById(R.id.pager);
      if (mPager == null)
          Log.i("Pager", "mPager = null");
      else 
          Log.i("Pager", "mPager = " + mPager.toString());
  
      Log.i("Pager", "Setting Pager Adapter");
      mPager.setAdapter(mAdapter);
  }
  
  public static class MyAdapter extends FragmentPagerAdapter {
      public MyAdapter(FragmentManager fm) {
          super(fm);
          Log.i("Pager", "MyAdapter constructor");
      }
  
      @Override
      public int getCount() {
          Log.i("Pager", "MyAdapter.getCount()");
          return NUM_ITEMS;            
      }
  
      @Override
      public Fragment getItem(int position) {
          Log.i("Pager", "MyAdapter.getItem()");
  
          return TestFragment.newInstance(position);
      }
  }
  
  public static class TestFragment extends Fragment {
  
      public static TestFragment newInstance(int position) {
          Log.i("Pager", "TestFragment.newInstance()");
  
          TestFragment fragment = new TestFragment();
  
          Bundle args = new Bundle();
          args.putInt("position", position);
          fragment.setArguments(args);
  
          return fragment;
      }
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          Log.i("Pager", "TestFragment.onCreateView()");
  
          LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.fragment_item, null);
          int position = getArguments().getInt("position");
  
          TextView tv = (TextView)layout.findViewById(R.id.text);
          tv.setText("Fragment # " + position);
          tv.setTextColor(Color.WHITE);
          tv.setTextSize(30);
  
          return layout;
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      &gt;
      &lt;android.support.v4.view.ViewPager
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>fragment_item.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      &gt;
      &lt;TextView  
      android:id="@+id/text"
      android:layout_width="match_parent" 
      android:layout_height="match_parent"
      android:gravity="center" 
      android:text="@string/hello"
      /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>My question is, instead of creating a new <code>Fragment</code> instance each time the user swipes left and right, how do I save a fragment's state (to some data structure) and then restore it?</p>
  
  <p>The <a href="https://android.googlesource.com/platform/development/+/master/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java" rel="noreferrer">API demo</a> doesn't seem to have any state information saving code at all.</p>