How to destroy Fragment?

I have one Activity. The Activity has two Fragments. Fragment A is Menu. Fragment B is Detail.

I try to Make other Fragment C in Fragment B, so, There are 3 Fragment in the Activity. And I try to Replace Fragment B to Fragment D.

I guess Fragment B and C is dead. BUT these Fragments is alive. Just Fragments are onDestroyView() state. I want onDestroy() or onDetach().

What do I do for Fragments.onDestroy() or onDetach()? I can't destroy or change the Activity.


If you don't remove manually these fragments, they are still attached to the activity. Your activity is not destroyed so these fragments are too. To remove (so destroy) these fragments, you can call:

fragmentTransaction.remove(yourfragment).commit()

Hope it helps to you


Give a try to this

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // TODO Auto-generated method stub

    FragmentManager manager = ((Fragment) object).getFragmentManager();
    FragmentTransaction trans = manager.beginTransaction();
    trans.remove((Fragment) object);
    trans.commit();

    super.destroyItem(container, position, object);
}