android -does calling finish from activity destroy hosted fragment?
Asked Answered
G

3

6

I have an 'Activity A' which host a 'Fragment B'. Please confirm that if I call A.finish() then 'fragment B 'gets destroyed. The fragment B would already be added to the fragment manager. I realize that a fragment is a sub-activity but i want to know do i have to tell the fragment manager to release the fragment in Ondestroy or is it all taken care of ?

Gentile answered 20/8, 2014 at 20:36 Comment(0)
B
9
but i want to know do i have to tell the fragment manager to 
release the fragment in Ondestroy or is it all taken care of ?

By default when you call finish in your activity it will automatically call onDestroy method of all the fragments that is attach to the activity thus destroying it, so no need to worry about destroying the fragment in onDestroy method of your activity.

From the documentation:

For example, when the activity is paused, so are all fragments in it, 
and when the activity is destroyed, so are all fragments. 
Birdiebirdlike answered 20/8, 2014 at 20:43 Comment(0)
L
0

Fragments are the Subactivities of an activity. So whenever we will call the finish the fragment associated with it will also get destroyed without notifying FragmentManager.

Lick answered 20/8, 2014 at 20:42 Comment(0)
T
0

Yes the fragment is destroyed. From the Android Fragment guide:

For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments.

Toothbrush answered 20/8, 2014 at 20:51 Comment(2)
Should people use onDestroy or onDestroyView ? And is the activity still available via getActivity on each of them?Bethany
@ android developer ondestroyview is good when the activity is stiill in memory. the fragments view is relased from memory but the fragment object itself remains until ondestroy is called.Gentile

© 2022 - 2024 — McMap. All rights reserved.