I am trying to understand some finer points of AsyncTaskLoaders. This may be obvious, to others but I can't find an unambiguous example or definition that demonstrates and exmplains what happens when you override the deliverResult()
method. What actually gets delivered ? How does this interact with the calling object ? I can see use of super.deliverResult
, which passes a private object from the class. So, does the loader automatically know what to associate with the "delivered result". I am totally confused.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
suppose when data are loading in the background, at this time, user press HOME button and exist the app, when user comes back to the app, loading has been finished. So we have already have the data, then
AsyncTaskLoader
will call thedeliverResult()
method, deliver the data to theonLoadFinished()
method for displaying.When the user come back to app,
onStartLoading()
is being called beforeloadInBackground()
. In this method, we could check if our data if empty or not, if not empty, we calldeliverResult()
and send the result toonLoaderFinished()
, so it could prevent to reload data.When we press HOME exist the app and then come back, it will not create a new Loader, instead the old loader will try to load data.
The only answer I can find that makes any sense is based on a decription in this link.
deliverResult appears to be used when you have listeners to the AsyncTask and want to send the results back to them. I would say it's uncommon. The Android documentation is even less descriptive:
deliverResult works after doInbackground completes. It sends the result D (returned by doInBackground) to the calling thread. You may wish to override it for cleaning data, but you can do clean-up in doInBackground instead without overriding deliverResult.
Seems I'm a bit late to the party, but anyway...
One of the main advantages of this intermediary step between the background loading and the UI thread's callback
onLoadFinished()
getting calledloadInBackground()
deliverResult()
andonLoadFinished()
is that it gives us a means of shortcutting the whole loading process from within the
AsyncTaskLoader
class. And this can be put to good use for caching the loading result within your AsyncTaskLoader and preventing the background loading from happening if there is cached data.And why would we want to do this? Isn't the whole point of loaders dealing with those dreaded activity lifecycle issues (e.g. rotating the device), maintaining state (like, caching data) and having a means to get updated when underlying data changes (CursorLoader)?
Well, yes, but this isn't the whole story.
Consider this use case:
You've got your app (the one with the AsynTaskLoader) up-and-running and it already has loaded data into your UI. Then, you switch over to your Twitter app to check on some news and return to you app. Without caching, upon returning to your app, the loader would do its reloading. This behavior is different from the one after configuration changes, e.g. rotating your device, in which case no reloading would take place.
So, how would we then prevent the loader from re-fetching data in case we're just sending our app to the background and, later, return to it again?
Solution
AsyncTaskLoader
implementation.deliverResult()
so that you save your fetched data in your cache first, before you call the superclass's implementation of deliverResult().onStartLoading()
check if there's cached data, and if so, let your AsyncTaskLoader just deliver that. Otherwise, start loading.Here's a link to a sample app which implements this behaviour. It's just a "Toy app" and as such part of Udacity's current version of the "Developing Android Apps" fundamentals course. And here is the link to the respective video within that course that deals with this issue. (The course is free, but you'll still have to sign-up w/ Udacity).
In short, what this app demonstrates, is a UI in which the user can input a search query for searching GitHub's repos (via the GitHub API), showing the resulting search URL in a TextView and also the raw JSON fetched from GitHub in another TextView.
The whole action happens in just
MainActivity.java
and the relevant part here is within theAsyncTaskLoader
that's implemented as an anonymous inner class:For step 1, just introduce a member variable in your
AsyncTaskLoader
implementation that's meant to serve as your data cache.For step 2, override
deliverResult()
as to cache the loading result.When
loadInBackground()
has finished, it passes its return value todeliverResult()
. It does so anyway, but now that we've overridden deliverResult() we can step right in and store our fetched data into the cache member variable which we've created with just so good foresight. And finally, we chain up to the super class implementation of deliverResult() withsuper.deliverResult()
which will pass-on the result to the callback methodonLoadFinished()
, running on the UI thread.For step 3, check in
onStartLoading()
whether or not we've got cached data.If we don't have cached data (yet), just force the loading to begin with a call to
forceLoad()
. But if we do have cached data, just calldeliverResult(yourCachedDataGoesHere)
and pass-in the cached data as argument.So, if you now switch back and forth between your app and some other app(s), you'll notice that no reloading takes place, as the loader will just use your cached data.