Sharing fragments in one activity?

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(); } 
+6
source share
4 answers

Do not mix Snippets created in XML and in code - bad things will happen. Save the container view in the layout, then add / replace fragments in it (it does not have the first fragment).

+3
source

It looks like me, I am doing something similar, although I am adding my initial fragment from the action using add , rather than loading it, referencing it in the layout. There may be a mistake. FYI I use the compatibility library.

It might be worth a try:

1) Add the transaction to the back stack to see if this has changed, it looks like you still want this functionality.

2) Give the fragment in the layout an identifier or tag, and then use it to execute remove and add fragB.

3) Try loading your fragA from code and see if this has changed.

0
source

Well, first of all, you can try using the newInstance () factory method to create an instance of fragment B, and not just the new FragB (). However, I think this is not a problem.

Can you just try not to use the fragment tag XML layout in yourself? Just do something like this:

 <RelativeLayout> <Linear/FrameLayout id="container" name="fragA"/> <ImageButton onClick="swapFragments" /> </RelativeLayout> 

Therefore, use either Frame or LinearLayout as the container for the fragment and inflate it in the onCreateView callback from the fragment. Maybe this helps let me know.

Hurrah!

0
source

As far as I can see, you are not hiding / detaching the previous fragment, so both will be displayed. You can implement something like this:

  if (mFragment != null) { ft = mActivity.getSupportFragmentManager().beginTransaction(); ft.hide(mFragment); ft.detach(mFragment); ft.commitAllowingStateLoss(); } 
0
source

Source: https://habr.com/ru/post/890619/


All Articles