title: weakreferences of fragments on Orientation Change using FragmentStatePagerAdapter -Android
body: <p>Im keeping track of fragments in a fragmentstatepageradapter using weakreferences to the fragments.  I found it cheaper then making a vector to keep track of the actual fragments.  So then i could call a function called getFragmentt(2) for example and it would search my list of weakreferences for fragment and if its alive it will return the real object.  Alls well so far.  Now if i rotate the device the fragmentManager recreates the fragments but the weakreferences are all lost as a result.  The adapter does not even get called when the fragments are getting re-created on a orientation change. Here is the code.</p>
  
  <pre><code>public class SectionsPagerAdapter extends FragmentStatePagerAdapter{
  
       ArrayList&lt;WeakReference&lt;Fragment&gt;&gt; m_fragments;
  
      public SectionsPagerAdapter(FragmentManager fm) {
          super(fm);
          m_fragments=new ArrayList&lt;WeakReference&lt;Fragment&gt;&gt;();
  
      }
  
      @Override
      public Fragment getItem(int position) {
          // getItem is called to instantiate the fragment for the given page.
          // Return a listFragment ) with the page number as one of its argument.
  
          TweetsListFragment mListfragment = TweetsListFragment.newInstance(
                  position, nf, mpageTitles[position]);
  
          m_fragments.add(position, new WeakReference&lt;Fragment&gt;(mListfragment));
  
          return mListfragment;
      }
  
      @Override
      public int getItemPosition(Object object) {
          printLog(Consts.TAG, "getting item position from pageview adapter");
          return PagerAdapter.POSITION_NONE;
      }
  
      @Override
      public int getCount() {
  
          return mpageTitles.length;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
  
          return mpageTitles[position];
  
      }
      /*return actual object from weak store of fragments on request*/
      public Fragment getFragment(final int position) {
          return m_fragments.get(position) == null ? null :
              m_fragments.get(position).get();
      }
  
      ......... etc
  </code></pre>