title: Does FragmentStatePagerAdapter save fragment state on orientation change?
body: <p>I have 3 fragments that need to be in a ViewPager. These fragment will hold dynamic information retrieved from a database. I understand that on an orientation change, the activity and fragments are destroyed and recreated. But I was under the impression by its name, that the FragmentStatePagerAdapter will save the state of the fragment. Apparently, I was wrong because every time I did something to the fragment, then change orientation, the fragment is reverted back to how it was laid out in the layout xml file.</p>
  
  <p>As I was debugging, I noticed that on orientation change, the Adapter's getItem() method was never invoked - meaning that it wasn't recreated. So then how come the fragment state reverted back to its original state?</p>
  
  <p>How do I save the fragment state using the FragmentStatePagerAdapter?</p>
  
  <p>Please note that I have been following this <a href="https://guides.codepath.com/android/ViewPager-with-FragmentPagerAdapter" rel="noreferrer">tutorial</a> and used their version of the <a href="https://gist.github.com/nesquena/c715c9b22fb873b1d259" rel="noreferrer">SmartFragmentStatePagerAdapter.java</a> class to manage the fragment dynamically.</p>
  
  <p>And the following are my sample codes.</p>
  
  <p><strong>PageLoader.java</strong> - This interface allows MainActivity to manage the loading of the fragment pages dynamically at run time.</p>
  
  <pre><code>public interface PageLoader {
      void loadPage(int from, int target);
  }
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements PageLoader {
      MyPagerAdapter adapter;
      DirectionalViewPager pager;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Get the ViewPager and set it's PagerAdapter so that it can display items
          pager = (DirectionalViewPager) findViewById(R.id.vpPager);
          adapter = new MyPagerAdapter(getSupportFragmentManager());
          pager.setOffscreenPageLimit(5);
          pager.setAdapter(adapter);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int position = pager.getCurrentItem();
          if (position &gt; 0) pager.setCurrentItem(position - 1);
          return true;
      }
  
      @Override
      public void loadPage(int from, int target) {
          PageLoader fragment = (PageLoader) adapter.getRegisteredFragment(target);
          fragment.loadPage(from, target);
      }
  }
  </code></pre>
  
  <p><strong>MyPagerAdapter.java</strong></p>
  
  <pre><code>public class MyPagerAdapter extends SmartFragmentStatePagerAdapter {
      private static final int NUM_ITEMS = 4;
  
      public MyPagerAdapter(FragmentManager fragmentManager) {
          super(fragmentManager);
      }
  
      // Returns total number of pages
      @Override
      public int getCount() {
          return NUM_ITEMS;
      }
  
      // Returns the fragment to display for that page
      @Override
      public Fragment getItem(int position) {
          switch (position) {
              case 0: // Fragment # 0 - This will show Frag1
                  return Frag1.newInstance(position, Frag1.class.getSimpleName());
              case 1: // Fragment # 0 - This will show Frag1 different title
                  return Frag1.newInstance(position, Frag1.class.getSimpleName());
              case 2: // Fragment # 1 - This will show Frag2
                  return Frag2.newInstance(position, Frag2.class.getSimpleName());
              default:
                  return Frag3.newInstance(position, Frag3.class.getSimpleName());
          }
      }
  
      // Returns the page title for the top indicator
      @Override
      public CharSequence getPageTitle(int position) {
          return "Page " + position;
  
      }
  }
  </code></pre>
  
  <p><strong>Frag1.java</strong> <strong>Frag2.java</strong> <strong>Frag3.java</strong> - these are all the same, except for the numbering.</p>
  
  <pre><code>public class Frag1 extends Fragment implements PageLoader {
      // Store instance variables
      private String title;
      private int page;
      private TextView txtView;
  
      // newInstance constructor for creating fragment with arguments
      public static Frag1 newInstance(int page, String title) {
          Frag1 fragmentFirst = new Frag1();
          Bundle args = new Bundle();
          args.putInt("someInt", page);
          args.putString("someTitle", title);
          fragmentFirst.setArguments(args);
          return fragmentFirst;
      }
  
      // Store instance variables based on arguments passed
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          page = getArguments().getInt("someInt", 0);
          title = getArguments().getString("someTitle");
      }
  
      // Inflate the view for the fragment based on layout XML
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.frag1, container, false);
          txtView = (TextView) view.findViewById(R.id.txt_frag1);
          txtView.setText(page + " - " + title);
  
          Button btn = (Button) view.findViewById(R.id.btn_frag1);
          btn.setOnClickListener( new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  PageLoader activity = (PageLoader) getActivity();
                  activity.loadPage(page, page+1);
  
              }
          });
          return view;
      }
  
      @Override
      public void loadPage(int from, int target) {
          txtView.setText(txtView.getText() + "\nThis message was created from" + from + " to " + target);
      }
  }
  </code></pre>
  
  <p><strong>activity_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:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;com.example.someone.smartfragmentstatepageradapter.custom.DirectionalViewPager
          android:id="@+id/vpPager"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"&gt;
  
      &lt;/com.example.someone.smartfragmentstatepageradapter.custom.DirectionalViewPager&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>frag1.xml</strong> <strong>frag2.xml</strong> <strong>frag3.xml</strong> - again these are all the same except for the numbering</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:background="#cc2"&gt;
  
      &lt;TextView
          android:id="@+id/txt_frag1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:text="txt_frag1"
          /&gt;
      &lt;Button
          android:id="@+id/btn_frag1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:text="btn_frag1"
          android:textSize="26dp" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>PLEASE tell me how I can use the FragmentStatePagerAdapter to save the "State" of my fragments. I've been scouring the internet from 9am to 9pm today... 12 hours...  I really need some help figuring this out. Thanks in advance!</p>