androidandroid-fragmentsandroid-tabhost

Remove old Fragment from fragment manager


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.


Solution

  • 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.