简体   繁体   中英

Android Actionbar tabs with fragments

In my application I have a single activity that instantiates an ActionBar with three Fragment classes.

// create action bar
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);

// set tabs
Tab tab = null;
tab = actionBar.newTab()
        .setIcon(R.drawable.tabbar_app_selected)
        .setTag(Globals.MainActivityTab.NewApp)
        .setTabListener(new MainActivityTabListener<BasicFormFragment>(this, "new_app_frag", BasicFormFragment.class, this));
actionBar.addTab(tab);

tab = actionBar.newTab()
        .setIcon(R.drawable.tabbar_saved)
        .setTag(Globals.MainActivityTab.Saved)
        .setTabListener(new MainActivityTabListener<SavedAppFragment>(this, "saved_apps_frag", SavedAppFragment.class, this));
actionBar.addTab(tab);

tab = actionBar.newTab()
        .setIcon(R.drawable.tabbar_settings)
        .setTag(Globals.MainActivityTab.Settings)
        .setTabListener(new MainActivityTabListener<SettingsFragment>(this, "settings_frag", SettingsFragment.class, this));
actionBar.addTab(tab);

During the runtime of my application there are times when I need to show a new fragment for a specific tab. Todo this I add a new fragment via the FragmentManager like below. This is to emulate a drill down type of interface that one would find in any mobile app.

// show report screen
AppReportOptionsFragment reportOptionsFragment = new AppReportOptionsFragment();
reportOptionsFragment.SetAppraisal(_app);
reportOptionsFragment.SetIsNew(_isNew);

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.SavedAppFrame, reportOptionsFragment);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack("AppReportOptionsFragment");
fragmentTransaction.commit();

This code works just fine, and I see my fragment take place of the previous fragment. I also retain the ability to change tabs, and the tab changes is the issue.

Basically, the problem I'm having is when I instantiate a new fragment like above, and then change to a different tab then the previous tab is reset to the original fragment. Thus clearing out any work that the user may have done on the previous tab.

My question is how do I force the application to retain the previous tab's sub-fragment so that when I tap on the tab again then the user will see the tab as they left it. Basically, I want this to work exactly how UITabbarController in iOS works.

Any suggestions on what am I doing wrong here, and is there a better way to implement a drill-down interface with Fragments and Actionbar tabs?

Thanks!

This answer doesn't resolve your issue. It is only a warning about new API. If you are using api 21 (it is not important if you are supporting old devices) you should consider to change your pattern because NAVIGATION_MODE_TABS is now deprecated.

You can use a pattern like the SlidingTabLayout in GoogleIO app.

Use Tab with new ToolBar (AppCompat v7-21)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM