title: How to save the state of a fragment in viewpager if I use fragments this way?
body: <p>I would like to save the state of a fragment in a viewpager. I know that I should do it this way: <a href="https://stackoverflow.com/questions/7951730/viewpager-and-fragments-whats-the-right-way-to-store-fragments-state">ViewPager and fragments — what&#39;s the right way to store fragment&#39;s state?</a> . But I use fragments this way:</p>
  
  <p><strong>MyAdapter</strong></p>
  
  <pre><code>class MyPagerAdapter extends FragmentStatePagerAdapter {
          public List&lt;String&gt; fragmentsA; //plus
  
          public MyPagerAdapter(FragmentManager fm) {
              super(fm);
              fragmentsA = fragments;//plus
          }
  
          @Override
          public Fragment getItem(int position) {
              //return MyFragment.newInstance();
              return Fragment.instantiate(context, fragmentsA.get(position));
  
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              //return CONTENT[position % CONTENT.length].toUpperCase();
              return mEntries.get(position % CONTENT.length).toUpperCase();
          }
  
          @Override
          public int getCount() {
             // return CONTENT.length;
              return mEntries.size();
          }
  
         @Override
          public int getItemPosition(Object object) {
              return POSITION_NONE;
         }
  
      }
  </code></pre>
  
  <p><strong>HostActivity</strong></p>
  
  <pre><code> public static List&lt;String&gt; fragments = new Vector&lt;String&gt;();
  
  @Override
      public void onCreate( Bundle savedInstanceState )
      {
  
  pager = (ViewPager)findViewById( R.id.viewpager );
  adapter = new MyPagerAdapter(getSupportFragmentManager());
  indicator = (TabPageIndicator)findViewById( R.id.indicator );
  pager.setAdapter( adapter );
  indicator.setViewPager( pager );
  pager.setCurrentItem(INITIAL_PAGE);
  
  //mEntries.add...
  
      fragments.clear();
      fragments.add( Tab1Fragment.class.getName());
      fragments.add( Tab2Fragment.class.getName());
      fragments.add( Tab3Fragment.class.getName());
      fragments.add( Tab4Fragment.class.getName());
  
  }
  </code></pre>
  
  <p>How can I save the fragmens state this way? I mean I need this method:</p>
  
  <pre><code>@Override
  protected void onSaveInstanceState(Bundle outState) {
  
      super.onSaveInstanceState(outState);
      getSupportFragmentManager()
              .putFragment(outState, MessagesFragment.class.getName(), mMessagesFragment);
  
  }
  </code></pre>
  
  <p>So where should I check for every fragment that <code>if (savedInstanceState != null)</code> ? And how should I put the fragments into the "outstate" ?</p>
  
  <p><strong>UPDATE</strong>
  So I put the on save state code to every fragment like this:</p>
  
  <pre><code>@Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      setUserVisibleHint(true);
      getFragmentManager().putFragment(outState, Tab1Fragment.class.getName(), this);
  } 
  </code></pre>