0

I am working on an application that contains three tabs and each tab has a fragment associated with it. On certain conditions i have to display child tab within second parent tab. But if that condition is not true then child tabs should not load and the parent tab should load one of the fragment i used in child tabs. For example, i have three fragments FragmentA, FragmentB, and FragmentC. Now if the condition is true then i will display tabs and load FragmentB and FragmentC inside FragmentA using Fragment tab host. But is condition is false i want to display FragmentB in FragmentA.

I tried using Fragment.replace() but it fails.

Ashwani Kumar
  • 834
  • 3
  • 16
  • 30
  • Hi i used this approach but it is not working for me:Fragment newFragment = new SecondFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.header, newFragment); transaction.addToBackStack(null); // Ads FirstFragment to the back-stack transaction.commit(); – Ashwani Kumar Oct 21 '13 at 08:52

3 Answers3

1

I think this is not a completely new issue. You can find an answer here: Separate Back Stack for each tab in Android using Fragments

Regards Francesco.

Community
  • 1
  • 1
user2270629
  • 144
  • 8
  • Hi Francesco, i am able to create the nested layout. That is child tabs under parent tab. But the issue i am facing is what is i don't need child tabs and instead of child tabs i just wanted to load a fragment for the parent tab. for ex if(loadchildtabs){ //code to load child tabs goes here} else{ //code for loading a fragment goes here} but in my case parent tab contains a fragment i have to replace parent tab fragment with another fragment. – Ashwani Kumar Oct 21 '13 at 09:24
  • If you want to let me see your code I can try to reproduce the issue. – user2270629 Oct 30 '13 at 08:40
1

In my intention you should override the onHiddenchange() for fragment A and check the condition over here.

Ganesh
  • 924
  • 2
  • 12
  • 34
0

I was able to solve the issue using a common fragment. In FragmentA's onCreateView if condition is true create Tabs using FragmentTabHost and add FragmentB and FragmentC to the tab. If condition is false then inflate a view using

    View fragmentView = Inflator.Inflate(R.id.fagment_layout, FragmentB); 
    FragmentB fragmentB = new FragmentB();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(fragmentView, fragmentB);
    transaction.commit();

It worked for me.

Ashwani Kumar
  • 834
  • 3
  • 16
  • 30