What is the use case for a Fragment with no UI?

Viewed 16969

The Android Developer guide has a decent section on the use of Fragments. One way to use Fragments is without a UI. There are a few references to using this as a means of background processing, but what advantages do Fragments bring to this area? Where would I choose to use a Fragment over Threads, AsyncTasks, Handlers, etc. for background processing?

5 Answers

A very useful feature of Headless Fragments

Headless Fragments, have one really useful feature - they can be retained by the FragmentManager across configuration changes. Since they do not have any UI related to them, they do not have to be detroyed and rebuilt again when the user rotates the device for example. In order to activate this behaviour, one just has to set the retained flag of the Fragment when it is initialized. This can be done in the onCreate method of the Fragment.


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

ref- https://luboganev.github.io/blog/headless-fragments/

Related