Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I know this has a few answers, but maybe this will help someone. I have used a relatively simple solution when I needed to get a <code>Fragment</code> from my <code>ViewPager</code>. In your <code>Activity</code> or <code>Fragment</code> holding the <code>ViewPager</code>, you can use this code to cycle through every <code>Fragment</code> it holds. </p> <pre><code>FragmentPagerAdapter fragmentPagerAdapter = (FragmentPagerAdapter) mViewPager.getAdapter(); for(int i = 0; i &lt; fragmentPagerAdapter.getCount(); i++) { Fragment viewPagerFragment = fragmentPagerAdapter.getItem(i); if(viewPagerFragment != null) { // Do something with your Fragment // Check viewPagerFragment.isResumed() if you intend on interacting with any views. } } </code></pre> <ul> <li><p>If you know the position of your <code>Fragment</code> in the <code>ViewPager</code>, you can just call <code>getItem(knownPosition)</code>.</p></li> <li><p>If you don't know the position of your <code>Fragment</code> in the <code>ViewPager</code>, you can have your children <code>Fragments</code> implement an interface with a method like <code>getUniqueId()</code>, and use that to differentiate them. Or you can cycle through all <code>Fragments</code> and check the class type, such as <code>if(viewPagerFragment instanceof FragmentClassYouWant)</code></p></li> </ul> <p><strong>!!! EDIT !!!</strong></p> <p>I have discovered that <code>getItem</code> only gets called by a <code>FragmentPagerAdapter</code> when each <code>Fragment</code> needs to be created the <strong>first time</strong>, after that, it appears the the <code>Fragments</code> are recycled using the <code>FragmentManager</code>. This way, many implementations of <code>FragmentPagerAdapter</code> create <strong>new</strong> <code>Fragment</code>s in <code>getItem</code>. Using my above method, this means we will create new <code>Fragment</code>s each time <code>getItem</code> is called as we go through all the items in the <code>FragmentPagerAdapter</code>. Due to this, I have found a better approach, using the <code>FragmentManager</code> to get each <code>Fragment</code> instead (using the accepted answer). This is a more complete solution, and has been working well for me.</p> <pre><code>FragmentPagerAdapter fragmentPagerAdapter = (FragmentPagerAdapter) mViewPager.getAdapter(); for(int i = 0; i &lt; fragmentPagerAdapter.getCount(); i++) { String name = makeFragmentName(mViewPager.getId(), i); Fragment viewPagerFragment = getChildFragmentManager().findFragmentByTag(name); // OR Fragment viewPagerFragment = getFragmentManager().findFragmentByTag(name); if(viewPagerFragment != null) { // Do something with your Fragment if (viewPagerFragment.isResumed()) { // Interact with any views/data that must be alive } else { // Flag something for update later, when this viewPagerFragment // returns to onResume } } } </code></pre> <p>And you will need this method.</p> <pre><code>private static String makeFragmentName(int viewId, int position) { return "android:switcher:" + viewId + ":" + position; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload