Removing fragments from an activity –

Development issue/problem:

FragmantClass rSum = new FragmantClass();
getSupportFragmentManager().beginTransaction().remove(rSum).commit() ;

I’m trying to remove this piece when I load the change to another piece. The above excerpt has not been deleted. This is the method I advocate for changing the slices.

public invalid switchContent(Fragment Fragment) {
FragmantClass rSum = new FragmantClass();
getSupportFragmentManager().startTransaction().remove(rSum).commit();
mContent = fragment;
getSupportFragmentManager()
.startTransaction()
.replace(R.id.content_frame, fragment)
.commit();
getSlidingMenu().showContent();
}

How can I solve this problem?

Solution 1:

getSupportFragmentManager().beginTransaction().
remove(getSupportFragmentManager().findFragmentById(R.id.frame)).commit() ;

Try this, it should work.

public invalid switchContent(Fragment) {
getSupportFragmentManager().beginTransaction().
remove(getSupportFragmentManager().findFragmentById(R.id.frame)).commit();
mContent = fragment;
getSupportFragmentManager()
.startTransaction()
.replace(R.id.content_frame, fragment)
.commit();
getSlidingMenu().showContent();
}

Solution 2:

In my personal case, this is how I add and remove an extract:

if(getSupportFragmentManager().findFragmentById(R.id.container) != null) {getSupportFragmentManager().beginTransaction(). remove(getSupportFragmentManager().findFragmentById(R.id.container)).commit();}.getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit() ;

Solution 3:

This is a very simple solution to the SupportFragmentManager. FragmentManager is not as convenient, but still effective:

List fragmentList = getSupportFragmentManager().getFragments();
// All fragments are accessed by their tag,
// in this case, just follow the line below to remove fragment
if (fragmentList == null) {
// code that handles non-existent fragments
}

for (fragment : fragmentList )
{
// To save one of the fragments, add this check
// when validating
, a tag can be added as a third parameter to the fragment if (frag.getTag().equals().

or if you are required to use it (but not recommended) :

.commitAllowingStateLoss() ;

If you repeatedly delete all fragments from the view, you must check that the current fragment is Null or isDetached() or isRemoving(), otherwise NullPointerExceptions may occur.

Update 6-9-15 : The documentation for getSupportFragmentManger().getFragments() is now apparently hidden, but still works well in my code. Here is a screenshot of the documentation:

Enter a description of the image here

Update 8-3-15 : Unfortunately, if you’re not using the fragment support library, there’s no way to use getFragments(), but there are a few other options.

  1. Give each fragment a tag or identifier when it is created, and repeat to treat each fragment as you wish.
  2. Create a listener with the onAttachListener, so that every time a new song is added to the activity, you can save that song and then iterate through this data structure to handle each song as you see fit.
See also  5 Marketing Mistakes When Starting Your Freelance Business

If you do not use getSupportFragmentManager(), you must use getFragmentManager() to process the transaction.

Solution 4:

In other words, you can replace the main content with an empty excerpt so that the main action is just displayed.

getFragmentManager()
.startTransaction()
.replace(main_content,new Fragment())
.commit() ;

Good luck!

Related Tags:

how to remove fragment from container in androidremove current fragment androidfragment remove and replacefragment lifecycleremove fragment from backstackfragment transaction removeremove fragment from fragmenthow to remove fragment from framelayout