1

I am new to android developing. And i have searched for this answer. Here’s what i know:

  1. Both AsyncTask and AsyncTaskLoaders do background processing in Android.
  2. The AsyncTaskLoader framework uses AsyncTask.
  3. In most cases, it’s better to perform background processes with the loader instead of Asynctask.
  4. AsyncTask has limitations such as destroying and recreating an activity during orientation changes and other configuration changes. And you can run out of memory of old AsyncTasks dwell in the system.
  5. You CAN use AsyncTask for short or interruptible tasks, tasks that don't need to report back to UI or user, and low-priority tasks that can be left unfinished. All other tasks need to be handled with the Loader.

My question is, do we only use one or the other in the same project or can we use both to handle different processes? If we can use both, can you give an example? If not, can you elaborate?

  • 1
    It is actually preferred to not use loaders anymore, the new preferred method is to use LiveData from the android architecture components. https://developer.android.com/topic/libraries/architecture/livedata.html – tyczj Feb 15 '18 at 16:47
  • Thanks! I’m taking beginner tutorials online. I guess they are teaching me strictly the basics right now. – Kelli Davis Feb 15 '18 at 16:49
  • 1
    here is a good article to read about why not to use loaders https://medium.com/inloop/its-time-to-ditch-loaders-in-android-6492616775f7 – tyczj Feb 15 '18 at 16:52
  • 1
    @tyczj LiveData solves a totally separate problem and isn't a replacement for loaders in many (most?) cases. And while I agree the Loader API isn't great, the ViewModel and LiveData implementations require just as much boilerplate. – Gabe Sechan Feb 15 '18 at 17:00
  • @GabeSechan can you please elaborate? In what case could we work with both loaders and LiveData? Can we work with all 3? – Kelli Davis Feb 15 '18 at 17:04
  • You can work with anything you want. The 3 aren't mutually exclusive. – Gabe Sechan Feb 15 '18 at 17:05
  • 1
    But as a short description- AsyncTask solves the problem of needing to do work off the main thread then do something on the main thread. It isn't just about the UI- its frequently about synchronizing access. A Loader is about not relaunching AsyncTasks (or other things) if its already in progress. This could be across activities as well as with activity restarts. LiveData is for when you want an observable data structure, its more similar to RxJava on a structure. It has nothing to do with Loaders, although it could be used to report the results of one to the main thread. – Gabe Sechan Feb 15 '18 at 17:10
  • @GabeSechan can you give an example? Like...what if i want to register or sign a user into my app then give the user access to a database of information that has multiple lists and sub lists? Or make a game? What tasks can i use each one for? – Kelli Davis Feb 15 '18 at 17:13
  • @GabeSechan Never mind. Your comment didn’t register on my app when i replied. Thanks. This really helps. – Kelli Davis Feb 15 '18 at 17:27
  • @GabeSechan "LiveData solves a totally separate problem and isn't a replacement for loaders in many (most?) cases" Google Android documentation states exactly the opposite: "Loaders have been deprecated as of Android P (API 28). The recommended option for dealing with loading data while handling the Activity and Fragment lifecycles is to use a combination of ViewModels and LiveData." https://developer.android.com/guide/components/loaders – Fran Marzoa Nov 17 '18 at 16:22

0 Answers0