2

I created three tabs using FragmentTabHost. Now I need to make all the navigation are under the tabs. How can I get this. I need to get what we get using TabGroupActivity.

Tab1 --->frag1-->frag2

Tab2 --->frag3

Tab3 --->frag4--->frag5

I have used fragmentTransaction.add(), fragmentTransaction.remove(), fragmentTransaction.replace(). These three methods but nothing give solution.

Replace method shows new fragment view on top of existing fragment view.

Remove and Add, from these two remove only works, add does not works.

Thanks in advance.

TabHostMain.java

    @Override
    protected void onCreate(Bundle savedInstanceState)
 {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.bottom_tabs);

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
                mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
            View view;
    view=getTabView(R.drawable.ic_launcher);

    Bundle b = new Bundle();
    b.putString("key", "Simple");
    mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator(view),Fragment1.class, null);

    b = new Bundle();
    b.putString("key", "Contacts");
    view=getTabView(R.drawable.ic_launcher);
    mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator(view), Fragment2.class, null);

    b = new Bundle();
    b.putString("key", "Custom");
    view=getTabView(R.drawable.ic_launcher);
    mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator(view),Fragment3.class, null);

}

Fragment3.java

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View view=LayoutInflater.from(getActivity()).inflate(R.layout.activity_second, null);

        ((TextView)view.findViewById(R.id.second_act_text)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fm=getChildFragmentManager();
                FragmentTransaction fragmentTransaction=fm.beginTransaction();
                fragmentTransaction.replace(R.id.second_activity, new Fragment1()).addToBackStack(null).commit();
            }
        });
        return view;
    }
SathishKumar
  • 1,644
  • 1
  • 14
  • 28

1 Answers1

2

The concept of different fragment navigation stacks in each tab has been discussed quite a few times on stackoverflow, one example for instance:

Separate Back Stack for each tab in Android using Fragments

One simple/crude way of achieving this without having to manage your own custom navigation back stack is to have a root fragment under each tab, and then whenever a root fragment wants to navigate to another fragment (fragment B) simply show a new Activity initially with fragment B and that Activity will have its own fragment navigation back stack.

Tab1 --->root frag1 --> Activity (own nav back stack) --> frag2

Tab2 --->root frag3

Tab3 --->root frag4 --> Activity (own nav back stack) --> frag5 --> frag6 --> frag7

An example of an app that does something like this is actually the StackAnywhere app. It makes heavy use of tabs, but when you navigate within those tabs it generally moves the navigation to a new Activity. YMMV with this approach, however.

Community
  • 1
  • 1
Stephen Asherson
  • 1,557
  • 14
  • 23