What's the difference between detaching a Fragment and removing it?
Asked Answered
P

2

126

In the Android docs for a FragmentTransaction I noticed two very similar methods: detach and remove. The descriptions over there don't seem to provide much insight about when to use each, and from what I can tell they appear to be the same.

So: what are the differences between these two methods?

Perichondrium answered 6/2, 2012 at 6:33 Comment(0)
I
172

The detach method removes the fragment from the UI, but its state is maintained by the Fragment Manager. This means you can reuse this fragment by calling the attach method, with a modified ViewHierarchy

Remove means the fragment instance cannot be re-attached. You will have to add it again to the fragment transaction.

Source Comment

You'll notice that when a Fragment is detached, its onPause, onStop and onDestroyView methods are called only (in that order). On the other hand, when a Fragment is removed, its onPause, onStop, onDestroyView, onDestroy and onDetach methods are called (in that order). Similarly, when attaching, the Fragment's onCreateView, onStart and onResume methods are called only; and when adding, the Fragment's onAttach, onCreate, onCreateView, onStart and onResume methods are called (in that order). – Adil Hussain

Illustrational answered 6/2, 2012 at 6:54 Comment(4)
To add to Rajdeep's answer, you'll notice that when a Fragment is detached, its onPause, onStop and onDestroyView methods are called only (in that order). On the other hand, when a Fragment is removed, its onPause, onStop, onDestroyView, onDestroy and onDetach methods are called (in that order). Similarly, when attaching, the Fragment's onCreateView, onStart and onResume methods are called only; and when adding, the Fragment's onAttach, onCreate, onCreateView, onStart and onResume methods are called (in that order).Extrorse
There is a fast Q&A with Diane Hackborn here. So why do I have this log? How do you know that FT.detach() has been called?Preventer
Whats the benefit of one over the other? I want to know a use case when one is favorable over the other? I always add and remove, is that bad?Casey
Best short and concise clarification.Irvingirwin
R
63

The naming of the fragment management methods are very confusing even according to Google engineers on message boards (see comments above). I made myself a little demo to figure out how things actually work. Here are my findings. Feel free to correct me if I am wrong.

To initially add a Fragment to an Activity, you use: getFragmentManager().beginTransaction().add(R.id.container, mFragment).commit().

This associates the Activity with the Fragment and also associates a View with the Fragment.

Here are the resulting life cycle events and other important method return values:

onAttach()           
onCreate()           
onCreateView()       
onViewCreated()      
onActivityCreated()  
onViewStateRestored()
onStart()            
onResume()

mFragment.getView() == null: false                    
mFragment.getActivity() == null: false

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

This removes any association with a View or to an Activity.

Here are the resulting life cycle events and other important method return values:

onPause()
onStop()
onDestroyView()
onDestroy()
onDetach()

mFragment.getView() == null: true
mFragment.getActivity() == null: true

I re-added the fragment here

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

This removes any association with a View, but keeps the association with the Activity.

Here are the resulting life cycle events and other important method return values:

onPause()                             
onStop()                              
onDestroyView()                      

mFragment.getView() == null: true
mFragment.getActivity() == null: false

To re-attach a Fragment that was detached to the Activity, you use: getFragmentManager().beginTransaction().attach(mFragment).commit().

This creates a new View to associate with the Fragment and maintains the Activity association.

Here are the resulting life cycle events and other important method return values:

onCreateView()                        
onViewCreated()                       
onActivityCreated()                   
onViewStateRestored()                 
onStart()                             
onResume()                            

mFragment.getView() == null: false
mFragment.getActivity() == null: false

Other important things to note: If you detach a Fragment and then try to add it again using add() rather than attach(), nothing seems to change.

if you attempt to add a Fragment that has been removed using remove() by using attach() rather than add(), nothing seems to change.

When getView() returns null, the Fragment may still have internal references to the last View it created. This View is no longer valid and should not be used.

Reedreedbird answered 11/3, 2014 at 9:12 Comment(3)
Good work. But it seemed quite interesting trying re-attaching and re-adding have the same effect after the fragment is removed.Eagan
So it turned out that "attach()" won't invoke onAttach(). "detach()" won't invoke onDetach().Betwixt
And some of these lifecycle events can slightly change if you keep transactions in the back stack.Eagan

© 2022 - 2024 — McMap. All rights reserved.