Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I still did not find a possibility to have a Pager container where fragments should be loaded in and also keep the (ActionBar) Tabs. I have however found a really dirty solution to acomplish this, with starting intens (Main Activity with the Tabs) and finishing the previous ones when the backbutton doesn't need it anymore.</p> <p>I adapted the code from <a href="https://github.com/JakeWharton/ActionBarSherlock" rel="nofollow">ABS: Support Demos</a> - Tabs and Pager. But again it's really dirty:</p> <p>LoaderCursorSupport.CursorLoaderListFragment under Tab2</p> <pre><code>@Override public void onListItemClick(ListView l, View v, int position, long id) { Intent intent = new Intent(); intent.setClass(getActivity(), ActionBarTabsPager.class); intent.putExtra("index", position); intent.putExtra("fragment", "details"); intent.putExtra("tab", 1); ActionBarTabsPager.mPreviousActivity = getActivity(); startActivity(intent); </code></pre> <p>ActionBarTabsPager (Main Activity with Tabs)</p> <pre><code>public class ActionBarTabsPager extends FragmentActivity { ViewPager mViewPager; TabsAdapter mTabsAdapter; static Activity mPreviousActivity; static Activity mActivity; static int mTabPosition = -1; static Boolean mTabRefreshed = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.actionbar_tabs_pager); getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); ActionBar.Tab tab1 = getSupportActionBar().newTab().setText("Tab 1"); ActionBar.Tab tab2 = getSupportActionBar().newTab().setText("Tab 2"); ActionBar.Tab tab3 = getSupportActionBar().newTab().setText("Tab 3"); ActionBar.Tab tab4 = getSupportActionBar().newTab().setText("Tab 4"); String fragment = ""; try { Bundle bundle = this.getIntent().getExtras(); fragment = bundle.getString("fragment"); mTabPosition = bundle.getInt("tab"); } catch (Exception ex) { } mViewPager = (ViewPager) findViewById(R.id.pager); mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager); mTabsAdapter.addTab(tab1, FragmentStackSupport.CountingFragment.class); if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.ECLAIR) { mTabsAdapter.addTab(tab2, FragmentStackSupport.CountingFragment.class); mTabsAdapter.addTab(tab3, FragmentStackSupport.CountingFragment.class); mTabsAdapter.addTab(tab4, FragmentStackSupport.CountingFragment.class); } else { if (!fragment.contains("details")) { mTabsAdapter.addTab(tab2, LoaderCursorSupport.CursorLoaderListFragment.class); } else { mTabsAdapter.addTab(tab2, ExampleFragment.class); } mTabsAdapter.addTab(tab3, LoaderCustomSupport.AppListFragment.class); mTabsAdapter.addTab(tab4, LoaderThrottleSupport.ThrottledLoaderListFragment.class); } if (savedInstanceState != null) { getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt("index")); } if (mTabPosition &gt; -1) { mTabsAdapter.setPrimaryItem(mTabPosition); mActivity = this; } } </code></pre> <p>Inside this Class there's a TabsAdapter</p> <pre><code>public static class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener { private final Context mContext; private final ActionBar mActionBar; private final ViewPager mViewPager; private final ArrayList&lt;String&gt; mTabs = new ArrayList&lt;String&gt;(); @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { if (mTabPosition &gt; -1 &amp;&amp; mTabRefreshed) { int tabPosition = tab.getPosition(); if (mTabPosition != tabPosition) { if (mPreviousActivity != null) { mPreviousActivity.finish(); mTabRefreshed = false; mPreviousActivity = null; mTabPosition = -1; Intent intent = new Intent(); intent.setClass(mContext, ActionBarTabsPager.class); intent.putExtra("fragment", "home"); intent.putExtra("tab", tabPosition); mActivity.startActivity(intent); mActivity.finish(); } } } mViewPager.setCurrentItem(tab.getPosition()); } </code></pre> <p>Can this be done simpler? Or should I just give up on having Tabs together with fragment history? This was done before Android 3.0 with ActivityGroups and Activities, but it seems this can't be done with fragments.</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.
    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