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

前端 未结 3 1931
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-28 15:08

Service

A Service is an application component representing either an application\'s desire to perform a longer-running operation while not interacting with the use

相关标签:
3条回答
  • 2020-12-28 15:39

    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.

    0 讨论(0)
  • 2020-12-28 15:43

    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.

    0 讨论(0)
  • 2020-12-28 15:45

    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.

    0 讨论(0)
提交回复
热议问题