Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 429
Chris Knight Avatar asked Jul 17 '12 22:07

Chris Knight


People also ask

Can you use a fragment without UI?

However, a fragment is not required to be a part of the activity layout; you may also use a fragment without its own UI as an invisible worker for the activity.

Can fragments be used without an activity?

It can't exist independently. We can't create multi-screen UI without using fragment in an activity, After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity.

What does the onCreateView () method return if a fragment doesn't have any UI?

These files contain only the onCreateView() method to inflate the UI of the fragment and returns the root of the fragment layout. If the fragment does not have any UI, it will return null.


1 Answers

A Fragment instance can persist through device configuration changes (like screen rotation). Because an Activity will be destroyed and recreated when a configuration change happens, it's difficult to design one that will keep track of a thread or AsyncTask. On the other hand, the system takes care of reattaching a persisted Fragment to the proper Activity at the other end (so to speak) of the configuration change. You would still be using a thread or AsyncTask, only now the Fragment is holding it instead.

There may be other uses for it, but there's the one I can think of.

like image 71
Karakuri Avatar answered Sep 24 '22 06:09

Karakuri