Remove old Fragment from fragment manager

khouloud mejdoub picture khouloud mejdoub · Mar 18, 2014 · Viewed 144.7k times · Source

I'm trying to learn how to use Fragments in android. I'm trying to remove old fragment when new fragment is calling in android.

Answer

Yashdeep Patel picture Yashdeep Patel · Mar 18, 2014

You need to find reference of existing Fragment and remove that fragment using below code. You need add/commit fragment using one tag ex. "TAG_FRAGMENT".

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

That is it.