Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found a slightly less gross way to get a handle on a <code>Fragment</code> created by a <code>FragmentPagerAdapter</code>. Instead of imitating the way tags are created, override <code>instantiateItem()</code>, get the <code>Fragment</code> returned by the superclass, and save the tag in <code>TabInfo</code>. This works for me on API >= 15, may work with lower. This still makes some assumptions about private code.</p> <pre><code>static final class TabInfo { private final Class&lt;?&gt; clss; private final Bundle args; private String tag; // ** add this TabInfo(Class&lt;?&gt; _class, Bundle _args) { clss = _class; args = _args; } } @Override public Object instantiateItem(ViewGroup container, int position) { final Fragment fragment = (Fragment) super.instantiateItem(container, position); final TabInfo info = mTabs.get(position); info.tag = fragment.getTag(); // set it here return fragment; } </code></pre> <p>Or in API &lt; 16, I think <code>instantiateItem()</code> takes a <code>View()</code> instead of <code>ViewGroup()</code>, like this:</p> <pre><code>public Object instantiateItem(View container, int position); </code></pre> <p>Then allow a way to get the <code>Fragment</code>, keeping the hack contained.</p> <pre><code>public Fragment getFragment(int index) { return mContext.getFragmentManager().findFragmentByTag(mTabs.get(index).tag); } </code></pre> <p>For that to work, the declaration for <code>mContext</code> needs to change to <code>Activity</code>, it's passed in as an <code>Activity</code> anyway:</p> <pre><code>private final Activity mContext; </code></pre>
    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