On the tablet there are two fragments (two different types of the same data), which are located next to each other. On mobile devices, we would like to switch between these two fragments with the click of a button. The mobile layout looks something like this:
<RelativeLayout> <fragment id="container" name="fragA"/> <ImageButton onClick="swapFragments" /> </RelativeLayout>
In the swapFragments(View) method, I'm trying to use the FragmentManager to replace fragA with fragB :
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.container, new FragB()); fragmentTransaction.commit();
... but I always see fragA through the transparent parts of fragB , making me believe that it just places fragB on top of fragA without replacing it.
I begin the journey of using hide(Fragment) and similar methods in a transaction, but this doesn't seem like the right way to do this.
Any tips on how to properly replace these fragments?
Edit : I saw the answer to this question . This bothers me, although I need to specify a different layout for the tablet and phone. If I need to add fragments programmatically, how can I avoid the code specific to each layout in the activity (i.e.
if(TABLET) { addFragmentA(); addFragmentB(); } else { addFragmentA(); }
source share