1

I have an app, fetchnig data from API and I use AsyncTaskLoader to update adapter and update UI with new data. Loader works fine, when rotating device (data is kept unchanged). Problem occurs, when I leave an app - using 'Home button' or turn off the screen and turn it back on. Then, data adapter and view are automatically updated with new, fetched data.

I have found out that when device is rotated - only onLoadFinished() method is executed. When I turn off/on screen - onStartLoading() and loadInBackground() Loader methods are executed before onLoadFinished()! I cannot understand why there is such a difference between device rotation and turning off/on screen. I believe that Loader should behave the same in these two situations - ONLY onLoadFinished() should be called.

EDIT: I managed to fix issue:
1. create global 'cache variable' in AsyncTaskLoader implementation
2. store fetched data in deliverResult method in global 'cache variable'
3. in onStartLoading() method check for cache variable, and if it isn't empty => deliverResult(cache variable)
More info: What does AsyncTaskLoader.deliverResult() actually do?

KKrzyzek
  • 101
  • 1
  • 7

1 Answers1

0

Yes loaders behavior got changed in 27.1

The underlying implementation of Loaders has been rewritten to use Lifecycle. While the API remains unchanged, there are a number of behavior changes

  • initLoader(), restartLoader(), and destroyLoader() can now only be called on the main thread.

    A Loader's onStartLoading() and onStopLoading() are now called when the containing FragmentActivity/Fragment is started and stopped, respectively.

    onLoadFinished() will only be called between onStart() and onStop. As a result, Fragment transactions can now safely be done in onLoadFinished().

    The FragmentController methods related to Loaders are now deprecated.

https://developer.android.com/topic/libraries/support-library/revisions

Check important changes in it

Sreedhu Madhu
  • 2,480
  • 2
  • 30
  • 40