title: How to get the instance of the currently visible fragment in ViewPager:
body: <p>I'm building an application that shows different fragments in a <code>ViewPager</code>. I add these fragments to the <code>ViewPager</code> like this:</p>
  
  <pre><code> public void intialiseViewPager() 
   {
      List&lt;Fragment&gt; fragments = new Vector&lt;Fragment&gt;();
      numberOfTabs = application.currentReport.getODTabsList().size();
      for (int i = 0; i &lt; numberOfTabs; i++)      
      {
          ODTab tempTab = application.currentReport.getODTabsList().get(i);
          if (tempTab.getTabType().equals(ODGrid.XML_GRID_ELEMENT))
          {
              GridFragment gridFragment = GridFragment.newInstance(tempTab.getTabId());
              fragments.add(gridFragment);
          }
          else  if (tempTab.getTabType().equals(ODChart.XML_CHART_ELEMENT))
          {
              NewChartFragment chartFragment = NewChartFragment.newInstance(tempTab.getTabId());
              fragments.add(chartFragment);
          }
      }
      Log.d(TAG, "Current report fragments set to adapter: "+fragments.toString());
      mPagerAdapter  = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
      mViewPager = (ViewPager)findViewById(R.id.pager);
      mViewPager.setAdapter(mPagerAdapter);
      mViewPager.setOffscreenPageLimit(0);
      mViewPager.setOnPageChangeListener(this);
  }
  </code></pre>
  
  <p>As you can see I pass to the fragment a String (<code>tempTab.getTabId()</code>), in this line:</p>
  
  <pre><code>GridFragment gridFragment = GridFragment.newInstance(tempTab.getTabId());
  </code></pre>
  
  <p>In the fragment itself I do this to initialize it:</p>
  
  <pre><code>public static final GridFragment newInstance(String tabId)
  {
      GridFragment f = new GridFragment();
      Bundle bdl = new Bundle(2);
      bdl.putString(TAB_ID, tabId);
      f.setArguments(bdl);
      return f;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
      String tabId = getArguments().getString(TAB_ID);
      if (application.currentReport != null)
      {
          this.odTab = application.currentReport.getODTabByTabId(tabId);
      }
      else
      {
          startActivity(new Intent(getActivity(), LoginScrActivity.class));
      }
      super.onCreate(savedInstanceState);
  }
  </code></pre>
  
  <p>All this is performed in order to not override the default empty constructor of the fragment, as I understand that this is not recommended.</p>
  
  <p>Now I need to get the instance of the <code>odTab</code> object that I put into the currently visible fragment in this line:</p>
  
  <pre><code>this.odTab = application.currentReport.getODTabByTabId(tabId);
  </code></pre>
  
  <p>Could someone please explain to me how this could be done?
  How do I get the currently visible fragment instance so I can pull the odTab object out of it?</p>
  
  <p><strong>UPDATE:</strong>
  With the suggestions I received here I have added an <code>odTab</code> object instance to the application class that is called <code>currentVisibleTab</code> and I'm setting the instance of the odTab like this:</p>
  
  <pre><code>@Override
  public void setUserVisibleHint(boolean isVisibleToUser) 
  {
      Log.d(TAG, "setUserVisibleHint invoked!");
      super.setUserVisibleHint(isVisibleToUser);
      if (isVisibleToUser) 
      {
          if (odTab != null &amp;&amp; getActivity() != null)
          {
              Log.d(TAG, "Currently visable tab: "+odTab.getTabTitle());
              application.currentVisibleTab = odTab;
          }
      }   
  }
  </code></pre>
  
  <p><strong>UPDATE2:</strong>
  This is my <code>ViewPagerAdapter</code>:</p>
  
  <pre><code>public class ViewPagerAdapter extends FragmentPagerAdapter 
  {
  private List&lt;Fragment&gt; fragments;
  
  /**
   * @param fm
   * @param fragments
   */
  public ViewPagerAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) {
      super(fm);
      this.fragments = fragments;
  }
  
  /* (non-Javadoc)
   * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
   */
  
  @Override
  public Fragment getItem(int position) {
      return this.fragments.get(position);
  }
  
  /* (non-Javadoc)
   * @see android.support.v4.view.PagerAdapter#getCount()
   */
  
  @Override
  public int getCount() {
      return this.fragments.size();
  }
  
  @Override
  public int getItemPosition(Object object) {
      return POSITION_NONE;
  } 
  
  public void removeAllFragments()
  {
      this.fragments.clear();
  }
  
  public void addFragmentsListToAdapter(List&lt;Fragment&gt; fragments)
  {
      this.fragments.addAll(fragments);
  }
  
  public List&lt;Fragment&gt; getFragments()
  {
      return fragments;
  }
  }
  </code></pre>
  
  <p>In it I have a <code>List</code> of fragments that are shown by the <code>ViewPager</code>. This list is initialized like this:</p>
  
  <pre><code> List&lt;Fragment&gt; fragments = new Vector&lt;Fragment&gt;();
  </code></pre>
  
  <p>What I don't understand is how I get a reference to the current fragment that triggered the interface method from this list. 
  And this is not a related question but maybe you know the answer: What is the difference between <code>List</code> and <code>Vector</code>?</p>
  
  <p>I still check this option out.</p>