What is the difference between a Headless Fragment and a Service in Android?
android, android-fragments, headless-fragments
Solution
`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.
Problem
Service A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Headless Fragment Fragments can be used without defining a user interface. It is recommended to use headless fragments for your background processing. What is the difference here?