A Service is an application component representing either an application\'s desire to perform a longer-running operation while not interacting with the use
Generally speaking:
Headless Fragments are meant to encapsulate data. Headless fragments are meant to encapsulate data which can be shared between various application components (since they can exist independently of a UI component).
Services are meant to encapsulate processing. They are more independent (and therefore also more heavy-weight, resource wise) than fragments are; they sit at a different level of abstraction and can last longer, in a system.
There is overlap between the two.
Service
s are given higher priority than Activity
s at the process-level. When memory is low, the Android system will prioritize Service
s over Activity
s, making Service
s the ideal option for long-running tasks. See the article titled Processes and Threads for more information.
Also, when you state the following in your original post:
Fragments can be used without defining a user interface. It is recommended to use headless fragments for your background processing.
Where are you quoting this from? I agree with the first sentence, but the second sentence is too general. For short-running tasks (such as performing HTTP requests, etc.), headless fragments work fine. However, for performing long-running background processing (such as downloading a very large file, etc.) a headless fragment may not be what you want. For example, if you used a headless fragment to perform a long-running task and the user clicked the "back button", this will cause both the Activity
and its headless Fragment
to be destroyed.
To summarize, a service is a background component that exists independent of an Activity
, meaning that it can continue to run in the background even if the Activity
which started the service is destroyed. On the other hand, a headless fragment will always have an associated parent Activity
. If the Activity
that hosts the fragment is destroyed by the system, then the fragment will have to be killed as well.
Headless fragment- fragment with no ui, basically used for storing large size object
Service - its long running task start by android, we can set priority to service even application get finished service will not stop unless its work get completed.