0

Since Tab Activity is deprecated I'm trying to implement tabs with fragments. As you can see in the lots of StackOverFlow questions back stack is an issue when you work with fragments and which has its own back stack.

So the thing I'm trying to do is, there is a fragment in each tab and this fragment can call another fragment within the same tab and its also the same for the other tabs.

Since there is only one activity then there is only one back stack for whole application. So I need to create my custom back stack which is separated for each tab. It's also the same common idea in the other questions. I need to find a way to create custom back stack but I couldnt find any example to take a look.

Is there any tutorial or any example piece of code doing something similar ? Thanks in advance.

osayilgan
  • 5,873
  • 7
  • 47
  • 68

2 Answers2

0

There is a backstack for the whole application, but there is also a backstack for fragments.

Perhaps have a read of this:

http://developer.android.com/guide/components/fragments.html#Transactions

When you perform a fragment transaction (add, replace or remove), you can add that transaction to the backstack.

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragmentContainer, fragment);
ft.addToBackStack(null);
ft.commit();

And now when you press back, the latest fragment will be 'popped' from the fragment backstack.

You could also override the onBackPressed(), and manage your fragment backstack there. (I'm currently having trouble trying to work out how to do this efficiently).

Anyway, there are several calls available from the FragmentManager to do with the backstack. The most useful to me being:

FragmentManager.getBackStackEntryCount() 

and

FragmentManager.PopBackStack()
Tim Malseed
  • 6,003
  • 6
  • 48
  • 66
  • 1
    thx for the answer, but this is not the thing that I'm asking exactly. Let me make it clear; Assume that I have a application with tab host and each tab is represented by a Fragment (not an activity). So the question is, assume that the user is in the first tab and user will call another fragment within the same tab (we are still in first tab). After that the user will change the tab to the second one and call another fragment with in the "second tab". So in the case user pushes back button, fragment manager will pop the latest Fragment – osayilgan Sep 25 '12 at 07:46
0

Sorry for late answer, but this might help somebody. I have added functionality like this in my project. I used fragment tabhost to add backstacks within it. Main logic will remain same in others.

Basically I took,

Stack<String> fragmentStack = new Stack<>();
boolean bBackPress = false;
String strPrevTab;
FragmentTabHost tabHost;

strPrevTab = "tag"; // Add tag for tab which is selected by default
        tabHost.setOnTabChangedListener( new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged( String tabId ) {
                if ( !bBackPress ) {
                    if ( fragmentStack.contains( tabId ) ) {
                        fragmentStack.remove( tabId );
                    }
                    fragmentStack.push( strPrevTab );
                    strPrevTab = tabId;
                }
            }
        } );

@Override
    public void onBackPressed() {
        if ( fragmentStack.size() == 0 ) {
            finish();
        } else {
            strPrevTab = fragmentStack.pop();
            bBackPress = true;
            tabHost.setCurrentTabByTag( strPrevTab );
            bBackPress = false;
        }
    }

Hope this helps!

Nidhi
  • 1
  • 1