A Loader can encapsulate practically anything, including Volley requests. When your Loader encapsulates a framework that already handles background work for you and calls you back on the main thread, like Volley, your loader implementation must not inherit from AsyncTaskLoader
but simply from the Loader
base class. You would then start the Volley request in the onForceLoad()
method.
When your loader gets the result back on the main thread through a callback, it just needs to push it to the Activity/Fragment by calling deliverResult()
.
Your loader would also need to keep a reference to the Volley request in progress to be able to cancel it in onStopLoading()
. onStopLoading() is not called in case of configuration change like screen rotation, only when leaving the Activity.
The only disadvantage is that Loaders don't have a built-in mechanism to propagate errors, while Volley does. So in your Volley error callback inside of your Loader, you'll need to either deliver a null result or send a local broadcast to notify the Activity/Fragment of the error.