Can't delete a fragment from FrameLayout?

I have a layout for landscape mode that shows a ListView on the left and a FrameLayout on the right. When an item is selected from the list, another fragment is added to FrameLayout

MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.myFrameLayout); FragmentTransaction ft = fragmentManager.beginTransaction(); if (myFragment == null) { myFragment = new MyFragment(uri); ft.replace(R.id.myFrameLayout, playerFragment); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.commitAllowingStateLoss(); } 

Later, I press delete in the list view and delete the last item in the list, and I try to delete the fragment so that nothing is displayed, but it does not work, my fragment remains on the screen. Code to remove:

 MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.myFrameLayout); FragmentTransaction ft = fragmentManager.beginTransaction(); ft.remove(myFragment); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); ft.commitAllowingStateLoss(); 

Any ideas why it is not being removed from the view?

+7
source share
6 answers

@DaveJohnston I tried your code to remove the fragment that I just molded .... thanks :)

Hey, I don't know if this is the solution to your problem or not, but you are trying in such a way that this will work:

Approach for adding a fragment:

 YourFragment yourFrament; //Add your Fragment public void addYourFragment(){ yourFragment = new YourFragment(); FragmentTransaction transation = getSupportFragmentManager().beginTransaction(); transation.replace(R.id.yourFlowLayout, yourFragment); transation.commit(); } 

Fragment removal approach:

  //Remove Fragment public void removeYourFragment(){ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if (yourFragment != null) { transaction.remove(yourFragment); transaction.commit(); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); yourFragment = null; } } 
+11
source

Do you declare a fragment in xml? Referring to goian's Dianne Hackborn:

Defining snippets in XML is mainly for things that will remain around. If you are about to add and remove, you should probably do this dynamically in sequence.

hope this helps

+3
source

When you find your fragment by identifier, you got a link to the fragment container, not the fragment. Use a TAG when you add a fragment, and then find a TAG:

 MyFragment myFragment = (MyFragment) fragmentManager.findFragmentByTag("yourTag"); 
0
source

It was very difficult for me to find the SO link to this problem that occurs when DO NOT install <fragment> via XML, but really dynamically set each Fragment using FragmentTransaction and <FrameLayout> .

Simplifying the Gru solution, the solution for me was simply adding .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) to my replacement transaction, and it worked! Previous snippets are now virtually hidden from view.

I assume other transitions will work as well.

0
source
 private static String TAG_FRAGMENT="MyFragment"; MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.myFrameLayout); FragmentTransaction ft = fragmentManager.beginTransaction(); if (myFragment == null) { myFragment = new MyFragment(uri); ft.replace(R.id.myFrameLayout, playerFragment,TAG_FRAGMENT); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.addToBackStack(TAG_FRAGMENT); ft.commit(); } 

when replacing a fragment with the use of the ft.addToBackStack(null) method after a simple removal of the fragment.

 Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT); if(fragment != null) getSupportFragmentManager().beginTransaction().remove(fragment).commit(); 
0
source

If you have a Fragment that is not deleted when replacing the Fragment in a FrameLayout ? you can delete it by making a separate delete transition using FragmentTransaction.TRANSIT_FRAGMENT_CLOSE on it before calling the regular replacement commit.

 Fragment removeThisFragment;//Keep a reference to the fragment you want to remove ... FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); // This fragment "sometimes" do not get replaced so we remove it here if it added. if(removeThisFragment!=null&&removeThisFragment.isAdded()) { transaction = fragmentManager.beginTransaction(); transaction.remove(removeThisFragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); transaction.commitAllowingStateLoss(); } // update the main content by replacing fragments transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.content_frame, fragment, TAG_MAIN_FRAGMENT).commit(); 

But I suggest that you first try to find out why the Fragment is not replaced in the first place, check if it does some hard work or something that forces it to be replaced? but if you need a quick temporary fix try the code above.

0
source

All Articles