25

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?

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Yesha
  • 381
  • 1
  • 4
  • 13

3 Answers3

50

Services are given higher priority than Activitys at the process-level. When memory is low, the Android system will prioritize Services over Activitys, making Services 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.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Thanks so much! I got the headless fragment information from the following link because there was no Android documentation on headless fragments. http://www.vogella.com/tutorials/AndroidFragments/article.html#headlessfragments1 – Yesha Apr 02 '14 at 01:50
  • 1
    Nice and very useful explanation Alex. – Ritesh Gune Dec 16 '16 at 09:17
  • @Alex Lockwood Services are given higher priority than Activities at the process-level I have read the link you have provided but does not found any information like this. – Muhammad Ahmed Ali Baig Jan 31 '18 at 14:42
36

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.

blueberryfields
  • 45,910
  • 28
  • 89
  • 168
2

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.

Mahesh
  • 2,862
  • 2
  • 31
  • 41