title: How to get fragment reference from ViewPager
body: <p>I am trying to get the reference to a specific Fragment called <code>MyChatsFragment</code> after instantiating it with a custom <code>SectionPageAdapter</code>.</p>
  
  <p>First, I am adding the Fragments</p>
  
  <pre><code>    mSectionsPageAdapter.addFragment(new CatalogFragment());
      mSectionsPageAdapter.addFragment(new MyBooksfragment());
      mSectionsPageAdapter.addFragment(new MyChatsFragment());
      viewPager.setAdapter(mSectionsPageAdapter);
  </code></pre>
  
  <p>Then I am trying to retrieve <code>MyChatsFragment</code> this way:</p>
  
  <pre><code> MyChatsFragment myChatsFragment = (MyChatsFragment) mSectionsPageAdapter
                                                                   .getRegisteredFragment(2);
  </code></pre>
  
  <p>Yet, <code>getRegisteredFragment(2)</code> returns <code>null</code> and while debugging I have noticed the following:</p>
  
  <p>In the Adapter, <code>instantiateItem</code> is only being called twice, therefore adding <code>CatalogFragment</code> and <code>MyBooksFragment</code> to <code>registeredFragments</code> SparseArray. This would explain why <code>getRegisteredFragment(2)</code> returns <code>null</code>. But why <code>MyChatsFragment</code> is actually created and the app is perfectly functional if <code>instantiateItem</code> is not even called for it?</p>
  
  <p><strong>Why isn't <code>MyChatsFragment</code> being added to <code>registeredFragments</code> SparseArray while other fragments are?</strong></p>
  
  <p>Is <code>getItem(position)</code> a viable solution?</p>
  
  <p>After checking <a href="https://stackoverflow.com/a/15261142/7201671">this answer</a>, I have adapted my <code>SectionsPageAdapter</code> by overriding <code>instantiateItem</code> and <code>destroyItem</code> as you will be able to check the below code:</p>
  
  <pre><code>public class SectionsPageAdapter extends FragmentPagerAdapter{
  private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
  private final SparseArray&lt;Fragment&gt; registeredFragments = new SparseArray&lt;Fragment&gt;();
  
  public void addFragment(Fragment fragment)
  {
      mFragmentList.add(fragment);
  }
  
  
  public SectionsPageAdapter(FragmentManager FM)
  {
      super(FM);
  }
  @Override
  public Fragment getItem(int position) {
     return mFragmentList.get(position);
  }
  
  @Override
  public int getCount() {
      return mFragmentList.size();
  }
  
  @Override
  public Object instantiateItem(ViewGroup container, int position) {
      Fragment fragment = (Fragment) super.instantiateItem(container, position);
      registeredFragments.put(position, fragment);
      return fragment;
  }
  
  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
      registeredFragments.remove(position);
      super.destroyItem(container, position, object);
  }
  
  public Fragment getRegisteredFragment(int position) {
      return registeredFragments.get(position);
      }
  }
  </code></pre>
  
  <p>Note: If needed, I can share <code>MyChatsFragment</code> code.</p>