What is the difference between a Headless Fragment and a Service in Android?-ThrowExceptions

Exception or error:

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?

How to solve:

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.

###

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.

###

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.

Leave a Reply

Your email address will not be published. Required fields are marked *