Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to attach/detach Android Fragments by hand?

I've read the Fragments documentation in depth and I haven't seen any references to the FragmentTransaction.attach() or FragmentTransaction.detach() methods. However, I've found multiple tutorials and demos where they actually use them (like FragmentTabs Demo). My question is:

  1. In theory, when are you supposed to attach/dettach Fragments by hand?
  2. What happens exactly when a Fragment is attached/detached? (is it created/destroyed? paused/resumed? etc?)
  3. Is it a good practice to attach/detach your fragments by hand?

Thank you!

like image 467
Andrés Pachon Avatar asked May 29 '12 15:05

Andrés Pachon


1 Answers

Fragments are being pushed by the Android development team as the way forward. There reason for their introduction is documented in various places on the web which I won't go into here. One reason for their introduction is to allow the developer to encapsulate specific functions of their applications into (almost) stand-alone modules that can be loaded and unloaded as appropriate and to maximise the wide range of device screens available to the Android platform.

Unfortunately (in my humble opinion) there are some subtleties that come with Fragments which can catch out the unwary developer (me being one of them). While I cannot claim to be any authority on Fragments I can pass on what I have discovered when using them. So to answer your questions:

I don't believe there are any hard-and-fast rules when you should attach()/detach() fragments by hand. However there may be a situation where you might want to detach a fragment rather than replace it with another. Note however this does not destroy the Fragment as stated in the docs

Detach the given fragment from the UI. This is the same state as when it is put on the back stack: the fragment is removed from the UI, however its state is still being actively managed by the fragment manager. When going into this state its view hierarchy is destroyed.

The Fragment object is destroyed but is visible element (if any) is. When the Fragment is re-attached with attach() its View hierarchy is re-created (docs):

Re-attach a fragment after it had previously been detached from the UI with detach(Fragment). This causes its view hierarchy to be re-created, attached to the UI, and displayed.

This is, as I have found, not particularly useful if your Fragment in the background needs to update it's View hierarchy so it's immediately up-to-date when your comes back into View. The end result is that you get an 'ugly' re-draw of your Fragment. I mean ugly in the sense that it gives your app the appearance of hurriedly and unprofessionally re-drawing itself rather than a much more desirable slick 'ready-to-go-from-the-off' air.

If maintaining an up-to-date Fragment UI is crucial then there are ways to avoid this re-draw. Instead of attaching and detaching your fragments you can simply show() and hide() them. This avoids the re-creation of your View hierarchy but you have to be careful that nothing in your View attempts to re-draw itself while the Fragment is hidden; This will result in an exception (I think. It's a while since I tinkered with this stuff).

There is nothing wrong by attaching and detaching Fragments by hand and you should probably think of these methods as being provided by the Android developers 'just-in-case' you need to do such a thing. Besides, I image replace() calls through to these more 'atomic' methods anyway.

As far as when Fragments are destroyed, obviously they are destroyed when an app closes but otherwise I suspect they just fall into the normal garbage routine (but don't quote me on that!). i.e. if there are no references to the object then it's marked for destruction.

like image 153
D-Dᴙum Avatar answered Oct 10 '22 22:10

D-Dᴙum