Note that there are some explanatory texts on larger screens.

plurals
  1. POActionBarSherlock fragment showing different data than passed
    text
    copied!<p>Hello and thank you for trying to help!</p> <p>I'm trying to build an app with ActionBarSherlcok. When I click the tabs, I would like the same fragment to instantiate but with dynamic data based on the tab I clicked. For some reason <strong>it keeps showing me the wrong data</strong>, though the correct parameter value is passed (I verified it using breakpoints and watches). <img src="https://i.stack.imgur.com/FWMzh.jpg" alt="correct parameter value but wrong data"></p> <p>I've read about SimpleOnPageChangeListener, getCurrentItem and of course the <a href="http://developer.android.com/guide/components/fragments.html" rel="nofollow noreferrer">Fragments Tutorial</a>. I relied on SwipeyTabs example to create this, and built a short demo to show my problem here:</p> <p>This is my MainActivity.java</p> <pre class="lang-java prettyprint-override"><code>package il.co.gilead.testdynamicfragments; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockFragmentActivity; import android.os.Bundle; import android.support.v4.view.ViewPager; public class MainActivity extends SherlockFragmentActivity { public static ViewPager mViewPager; private TabsAdapter mTabsAdapter; Integer intNumOfPages = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mViewPager = new ViewPager(this); mViewPager.setId(R.id.pager); setContentView(mViewPager); final ActionBar bar = getSupportActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); mTabsAdapter = new TabsAdapter(this, mViewPager); for (int i=1; i&lt;=intNumOfPages; i++) { mTabsAdapter.addTab(bar.newTab().setText("Fragment "+i), TestFrag.class, null); } } } </code></pre> <p>This is my TestFrag.java</p> <pre class="lang-java prettyprint-override"><code>package il.co.gilead.testdynamicfragments; import com.actionbarsherlock.app.SherlockFragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class TestFrag extends SherlockFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.frag_test, container, false); TextView tv = (TextView) v.findViewById(R.id.textView1); Integer pos = (MainActivity.mViewPager.getCurrentItem() + 1); tv.setText("Page " + pos.toString()); return v; } } </code></pre> <p>My activity_main.xml</p> <pre class="lang-java prettyprint-override"><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" &gt; &lt;android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pager" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>and my test_frag.xml</p> <pre class="lang-java prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Finally my TabsAdapter</p> <pre class="lang-java prettyprint-override"><code>public 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;(); 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 fa, ViewPager pager) { super(fa.getSupportFragmentManager()); mContext = fa; mActionBar = fa.getSupportActionBar(); 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); tab.setTabListener(this); mTabs.add(info); mActionBar.addTab(tab); notifyDataSetChanged(); } @Override public void onPageScrollStateChanged(int state) { } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { mActionBar.setSelectedNavigationItem(position); } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { mViewPager.setCurrentItem(tab.getPosition()); Object tag = tab.getTag(); for (int i = 0; i&lt;mTabs.size(); i++){ if (mTabs.get(i) == tag){ mViewPager.setCurrentItem(i); } } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } @Override public Fragment getItem(int position) { TabInfo info = mTabs.get(position); return Fragment.instantiate(mContext, info.clss.getName(), info.args); } @Override public int getCount() { return mTabs.size(); } } </code></pre> <p>If you'll play around with it, you'll see that clicking tab "Fragment 2" sometimes shows "Page 1" and sometimes shows "Page 3" and sometimes shows "Page 2". I think it has something to do with pre-loading the fragments, or fragment refresh, but at this point I am clueless...</p> <p>Thanks again for your help!</p>
 

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