0

I've been reading on how to execute background tasks that communicate with the UI and I came across these two solutions. But I'm not decided on which one I should go with for the best option.

1. AsyncTask in a Fragment with setRetainInstance(true)

Following the guide in this page, this seems to take really good care of orientation changes and memory leaks.

2. AsyncTaskLoader is what google recommends, but there are some problems, like this, and documentation is also very sparse.

I'm actually more inclined to use option #1, but I don't know the full advantages/disadvantages compared to google's recommendation of #2.

Any input is appreciated.

Community
  • 1
  • 1
Poly Bug
  • 1,514
  • 1
  • 14
  • 27
  • 1
    reading this https://medium.com/google-developers/making-loading-data-on-android-lifecycle-aware-897e12760832#.khtvudnkr might help. i prefer option 2 – Raghunandan Apr 20 '16 at 14:32
  • there is no issue with the loaders. you need to store your data same where. loaders take care of configuration changes – Raghunandan Apr 20 '16 at 14:36

1 Answers1

0

These days you typically want to go with the AsyncTaskLoader over an AsyncTask because:

  1. They tie in better with the Fragment life cycle & configuration changes.
  2. Consequently you're less likely to run into NullPointerExceptions when doing stuff with the UI with an AsyncTaskLoader.

One problem is that AsyncTaskLoaders don't have an obvious solution to updating progress bars periodically whereas AsyncTask has the onProgressUpdate(...) method.

Also keep in mind that you could simply just use setRetainInstance(true) AND use AsyncTaskLoaders. Por que no los dos? ;) (note that setRetainInstance(true) is arguably not always a good idea because it can cause Context leaks)

Patrick
  • 1,045
  • 1
  • 9
  • 21