0

I am trying to avoid using intents and activities within tabhost and tabwidget

This code is starting to get messy just using views for everything.

My problem is that I am having difficulty retaining information. In one tab I have one view which has 4 buttons, each button loads another view and this takes up the visual information on screen in that tab. The problem is that when I go "back" to load the previous view in that tab, none of the information is that view is retained, even the button listeners won't re-instantiate.

How do I approach this? I have seen some very rudimentary examples of views within tabs, but nothing interactive that loads more views.

(viewflippers and action bars are not an option, and I am trying to avoid using tabs with activities)

CQM
  • 42,592
  • 75
  • 224
  • 366

2 Answers2

2

Forget Activity Group. Forget Tab Host. It's all about ActionBar Tabs and ViewPager Fragments now. The API Demos sample app (which is also the Android Compatibility Library sample app) provides an implementation that combines both Tab and ViewPager navigation between Fragments, but it sort of fakes the Tabs (i.e., they are not true ActionBar tabs). See FragmentTabsPager.java. You can take this and make it work with true ActionBar tabs, too.

Here's the interesting bit. (I deleted a bunch of stuff, so don't look for a complete working solution here.)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_tabs_pager);

    mViewPager = (ViewPager)findViewById(R.id.pager);

    // This block thanks to https://stackoverflow.com/q/9790279/517561
    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowTitleEnabled(true);
    bar.setDisplayShowHomeEnabled(true);
    //

    mTabsAdapter = new TabsAdapter(this, mViewPager);

    mTabsAdapter.addTab("simple", "Simple", 
            FragmentStackSupport.CountingFragment.class, null);
    mTabsAdapter.addTab("contacts", "Contacts",
        LoaderCursorSupport.CursorLoaderListFragment.class, null);
    mTabsAdapter.addTab("custom", "Custom",
        LoaderCustomSupport.AppListFragment.class, null);
    mTabsAdapter.addTab("throttle", "Throttle",
        LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
}

public static class TabsAdapter extends FragmentPagerAdapter
        implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
    private final SherlockFragmentActivity mContext;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        @SuppressWarnings("unused")
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(String _tag, Class<?> _class, Bundle _args) {
            tag = _tag;
            clss = _class;
            args = _args;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;

        public DummyTabFactory(Context context) {
            mContext = context;
        }

        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(String tag, CharSequence label, Class<?> clss, Bundle args) {
        ActionBar.Tab tab = mContext.getSupportActionBar().newTab();
        tab.setText(label);
        tab.setTabListener(this);
        mContext.getSupportActionBar().addTab(tab);
        TabInfo info = new TabInfo(tag, clss, args);
        mTabs.add(info);
        notifyDataSetChanged();    
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    @Override
    public void onPageSelected(int position) {
        mContext.getSupportActionBar().setSelectedNavigationItem(position);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
      mViewPager.setCurrentItem(mContext.getSupportActionBar().getSelectedNavigationIndex());
    }
}

My complete solution: http://code.google.com/p/sherlock-demo/

Note: Requires ActionBarSherlock. Note: Thanks to ActionBarSherlock and FragmentTabsPager

Community
  • 1
  • 1
Sparky
  • 8,437
  • 1
  • 29
  • 41
  • okay, now give me an implementation that works on Android 2.2+ and has tabs/"actionbars" at the bottom of the screen. Challenge accepted? – CQM May 07 '12 at 19:31
  • on my android 2.3.3 phone, half of the android v4 support sample classes crash with a "ClassNotFoundException" – CQM May 07 '12 at 19:42
  • See solution in updated answer. Don't have a phone handy, but runs in a 2.1 AVD. – Sparky May 07 '12 at 21:17
  • Oh, forgot to mention: I chose to pass on the bottom tabs, because that goes against http://developer.android.com/design/patterns/actionbar.html . – Sparky May 07 '12 at 21:33
-1

User Activity Group for tabs and put any beautiful layout you create for tab or activities.

Here we go: http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html

Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105
  • I know how to do activities in tabs, but I thought using intents in tabs was bad practice which is why google ultimately deprecated the use of tabactivity altogether. so I was trying to use just plan views and layoutInflater but it seems too limited – CQM May 07 '12 at 19:07