Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 251
Ignition Avatar asked Aug 19 '11 08:08

Ignition


People also ask

How do you end a fragment?

If you are using dynamic fragments, set up via a FragmentTransaction , you could run a transaction to replace() the old fragment with the new one.

How to detach a fragment?

To detach an added Fragment from an Activity, you use: getFragmentManager(). beginTransaction(). detach(mFragment). commit().

How to replace fragment in fragment?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();


1 Answers

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

like image 88
Nhat Nam NGUYEN Avatar answered Sep 27 '22 21:09

Nhat Nam NGUYEN