0

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!

miken.mkndev
  • 1,821
  • 3
  • 25
  • 39
  • That's not a very productive response, and quite rude to be honest. This is place for people to come and get help, not abuse. Furthermore, you should really think before you speak and perhaps ask for more information. This was in fact depreciated in API level 21, but my application supports API level 13 and up. Therefore, this constant is not deprecated in the SDK I am using. – miken.mkndev Mar 03 '15 at 21:58
  • Sorry.You are right. My comment (it is not an 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. – Gabriele Mariotti Mar 03 '15 at 22:20
  • Thank you. Right now I am only supporting up to API level 19 as my requirement was to support as low as API level 13, and I didn't want to run into issues using newer SDK methods that aren't supported on older devices. I also have a requirement to use a tabbed navigation interface as this is a port from an iOS app, and the client wants a near identical interface. I agree this is not the best approach when developing native apps, but clients have the final say unfortunately. With that said what pattern would you recommend and still be able to support tabbed navigation? – miken.mkndev Mar 03 '15 at 22:30
  • And please keep in mind I'm new to the Android world. I'm an iOS developer/user trying to make heads and tails of the native user interface on Android, so I may be making the completely wrong calls here. lol – miken.mkndev Mar 03 '15 at 22:31
  • It can be a valid pattern. This example is from the google io app.http://stackoverflow.com/questions/26540078/use-tab-with-new-toolbar-appcompat-v7-21/26543020#26543020 – Gabriele Mariotti Mar 03 '15 at 22:35
  • Ok, I appreciate your input and will heed your advice. If you don't mind please write up a quick "answer" with the link above and I'll be happy to mark it as the accepted answer so you credit for it:) – miken.mkndev Mar 04 '15 at 14:19

1 Answers1

1

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)

Community
  • 1
  • 1
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • While this doesn't answer my question it does point me in the right direction to find the correct solution for my problem. Thank you! – miken.mkndev Mar 05 '15 at 10:52