1

J'ai un fragment dans un autre et j'ajoute et je le commute succussfully, mais quand je veux l'enlever, ça ne marche pas et c'est toujours où il était.Impossible de supprimer un fragment enfant dans un autre

Activité principale Fragment

public class MainActivityFragment extends Fragment { 
    ScreenUtility screenUtility; 

    public MainActivityFragment() { 
    } 

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

     View v = inflater.inflate(R.layout.fragment_main, container, false); 

     screenUtility = new ScreenUtility(getActivity()); 

     TotalAmountFragment totalAmountFragment; 
     totalAmountFragment = new TotalAmountFragment(); 

     if (screenUtility.getOrientation() == 1){ 
      getChildFragmentManager() 
        .beginTransaction() 
        .add(R.id.main_total_amount_fragment_container, totalAmountFragment, "totalPrice") 
        .commit(); 
     } else if (screenUtility.getOrientation() == 2 && getChildFragmentManager().findFragmentByTag("totalPrice") != null){ 
      FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); 
      transaction.remove(totalAmountFragment); 
      transaction.commit(); 
      transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); 
     } 
     return v; 
    } 
} 

fichier XML

... 
<FrameLayout 
      android:id="@+id/main_total_amount_fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:foregroundGravity="center" 
      android:layout_centerHorizontal="true"/> 

Que dois-je faire? J'ai fait tout ce que je pensais.

Répondre

0

Essayez ce code

FragmentManager fm = getActivity().getSupportFragmentManager(); 
if (fm.getBackStackEntryCount() != 0) { 
     fm.popBackStack(); 
    } 
+0

ne fonctionne pas! Lorsque la tablette est en mode paysage au premier démarrage, le fragment n'est pas ajouté, et lorsque je le fais pivoter en mode portrait, le fragment enfant apparaît correctement, mais lorsque je le remonte en mode paysage, il ne disparaît pas! – altruistic

+0

Pourquoi ne remplacez-vous pas tout de suite le fragment? – Madhav

+0

Remplacer par quoi? – altruistic

0
if (screenUtility.getOrientation() == 1){ 
     getFragmentManager() 
       .beginTransaction() 
       .add(R.id.main_total_amount_fragment_container, totalAmountFragment, "totalPrice") 
       .commit(); 
    } else { 
     Fragment priceFragment = getActivity().getSupportFragmentManager().findFragmentByTag("totalPrice"); 
     if (priceFragment != null){ 
      getActivity().getSupportFragmentManager().beginTransaction().remove(priceFragment).commit(); 
     } 
    }