Fragment transaction remove

Viewed 2140

I have a fragment, which I add with a tag like this:

getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,fragment,fragment_tag).commit();

At some point, in another fragment, I need to remove this fragment so I call:

fragment = getActivity().getSupportFragmentManager().findFragmentByTag(fragment_tag);
if(fragment != null && fragment.isAdded())
    fragmentManager.beginTransaction().remove(fragment).commit();

In theory, the next time I call findFragmentByTag() for the fragment I just removed, I should get a null, however, the next time i call this:

fragment = getActivity().getSupportFragmentManager().findFragmentByTag(fragment_tag);
if(fragment != null)

The if statement is true so it actually finds a fragment with that tag!

My question is, how do I COMPLETELY remove a fragment?

Take in consideration that the fragment to be removed may not be in the top of the backstack, that meaning that popBackStack() is not a solution.

2 Answers
int index = getSupportFragmentManager().getFragments().indexOf(oldfragment);
        getSupportFragmentManager().getFragments().set(index, null);
Related