Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid TabsAdapter handle fragments
    primarykey
    data
    text
    <p>I want to handle one fragment within tabsadapter which is a static class in my Activity. The problem is how can i get the <a href="/questions/tagged/getfragmentbytag" class="post-tag" title="show questions tagged 'getfragmentbytag'" rel="tag">getfragmentbytag</a> in the onTabSelected?</p> <p>From the onCreate i have this code:</p> <pre><code>Tab tab1 = actionBar.newTab().setText("ABOUT"); tabsAdapter.addTab(tab1, PoiAboutFragment.class, null); </code></pre> <p>and my tabs adapter class is the following which works with no problem.</p> <pre><code>public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener { private final Context mContext; private final ActionBar mActionBar; private final ViewPager mViewPager; private final ArrayList&lt;TabInfo&gt; mTabs = new ArrayList&lt;TabInfo&gt;(); private Object gridTag; static final class TabInfo { private final Class&lt;?&gt; clss; private final Bundle args; TabInfo(Class&lt;?&gt; _class, Bundle _args) { clss = _class; args = _args; } } public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager, ActionBar actionBar) { super(activity.getSupportFragmentManager()); mContext = activity; mActionBar = actionBar; mViewPager = pager; mViewPager.setAdapter(this); mViewPager.setOnPageChangeListener(this); } public void addTab(ActionBar.Tab tab, Class&lt;?&gt; clss, Bundle args) { TabInfo info = new TabInfo(clss, args); tab.setTag(info); if(clss.equals(GridFragment.class)) gridTag = tab.getTag(); tab.setTabListener(this); mTabs.add(info); mActionBar.addTab(tab); notifyDataSetChanged(); } @Override public int getCount() { return mTabs.size(); } @Override public Fragment getItem(int position) { TabInfo info = mTabs.get(position); return Fragment.instantiate(mContext, info.clss.getName(), info.args); } public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } public void onPageSelected(int position) { mActionBar.setSelectedNavigationItem(position); } public void onPageScrollStateChanged(int state) { } public void onTabSelected(Tab tab, FragmentTransaction ft) { Object tag = tab.getTag(); Log.i(TAG, "onTabSelected has been called"); if(tag.equals(gridTag)) Log.i(TAG, "gridtab has been called"); for (int i = 0; i &lt; mTabs.size(); i++) { if (mTabs.get(i) == tag) { mViewPager.setCurrentItem(i); } } } </code></pre> <p>the problem is that i want to handle one of my fragments in both the oncreate method and in the tabsAdapter but i don't have neither a fragment tag neither an fragment Id. How can i handle their methods from my Activity?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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