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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With