Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you ask me, the second solution on the below page, keeping track of all the "active" fragment pages, is better: <a href="http://tamsler.blogspot.nl/2011/11/android-viewpager-and-fragments-part-ii.html" rel="noreferrer">http://tamsler.blogspot.nl/2011/11/android-viewpager-and-fragments-part-ii.html</a></p> <p>The answer from barkside is too hacky for me.</p> <p>you keep track of all the "active" fragment pages. In this case, you keep track of the fragment pages in the FragmentStatePagerAdapter, which is used by the ViewPager.</p> <pre><code>private final SparseArray&lt;Fragment&gt; mPageReferences = new SparseArray&lt;Fragment&gt;(); public Fragment getItem(int index) { Fragment myFragment = MyFragment.newInstance(); mPageReferences.put(index, myFragment); return myFragment; } </code></pre> <p>To avoid keeping a reference to "inactive" fragment pages, we need to implement the FragmentStatePagerAdapter's destroyItem(...) method:</p> <pre><code>public void destroyItem(View container, int position, Object object) { super.destroyItem(container, position, object); mPageReferences.remove(position); } </code></pre> <p>... and when you need to access the currently visible page, you then call:</p> <pre><code>int index = mViewPager.getCurrentItem(); MyAdapter adapter = ((MyAdapter)mViewPager.getAdapter()); MyFragment fragment = adapter.getFragment(index); </code></pre> <p>... where the MyAdapter's getFragment(int) method looks like this:</p> <pre><code>public MyFragment getFragment(int key) { return mPageReferences.get(key); } </code></pre> <p>"</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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