1

I used to use async tasks for background tasks but later found out that services are recommended instead as they have longer life.

Now, reading about fragments I found out you can use fragments without ui to run background tasks.

I have seen this: Application threads vs Service threads

which only talks about the life-time difference between threads and services.

am confused here! when planning to use background tasks what are the appropriate times when to use services, fragments without UI or the normal java Threads?

Community
  • 1
  • 1
dsharew
  • 10,377
  • 6
  • 49
  • 75
  • 3
    Does this answer your question? [What is the use case for a Fragment with no UI?](https://stackoverflow.com/questions/11531648/what-is-the-use-case-for-a-fragment-with-no-ui) – Viks Feb 01 '21 at 22:20

1 Answers1

0

Fragment can persist through device configuration changes like screen rotation. Because an Activity will be destroyed and recreated when a configuration change happens, it is difficult to design one that will keep track of a THREAD or ASYNCTASK . On the other hand, the system takes care of reattaching a persistent Fragment to the proper Activity at the other end of the configuration change. You would still be using a THREAD or ASYNCTASK, only now the Fragment is holding it instead.

vishal jangid
  • 2,967
  • 16
  • 22
  • So just to be clear are you saying fragment can persist after screen rotation, this seems to contradict with http://stackoverflow.com/questions/22799759/what-is-the-difference-between-a-headless-fragment-and-a-service-in-android – dsharew Apr 28 '15 at 13:14