addListener
Adds a RequestListener to the list that will be called in the order they were added when the request ends.
Multiple calls to this method append additional listeners. Previous listeners are not removed. If you want to replace any previously added listeners, use listener.
Listeners track the state of the request started by this particular builder
. When used with the thumbnail APIs (thumbnail) this can start to seem confusing because multiple requests are running and each may succeed or fail, independent of each other. As a rule, Glide does not add RequestListeners to thumbnail requests automatically. That means that RequestListeners track the state of exactly one request in the chain. For example, if you start a primary request with a single nested thumbnail and you add a RequestListener only to the primary request, then the RequestListener will only be notified when the primary request succeeds or fails. If the thumbnail succeeds, but the primary request fails, the RequestListener added to the primary request will still be called with onLoadFailed. In the same scenario, the RequestListener added only to the primary request will not have onResourceReady called when the thumbnail request finishes successfully. Similarly, if you add a RequestListener only to a thumbnail request, but not the primary request, that
listener
will only be called for changes related to the thumbnail request. If the thumbnail request fails, the listener
added to the thumbnail request will be immediately called via onLoadFailed, even though the primary request may eventually succeed. It is perfectly possible to add a to both the primary and a thumbnail request. If you do so, the will be called independently for each request when it finishes. Keep in mind that if any parent request finishes before its thumbnail request(s), it will attempt to cancel those requests. As a result there's no guarantee that a RequestListener added to a thumbnail request will actually be called with either success or failure. These same patterns hold for arbitrarily nested thumbnails. The listener
is only called for the requests it is added to and may not be called for every thumbnail request if those requests are cancelled due to the completion of a parent request.
The one exception to the rules about thumbnails is thumbnail. In this case we appear to be passing RequestListeners added to the parent request to the generated thumbnail requests. To try to reduce confusion, the thumbnail method has been deprecated. It can be easily replicated using thumbnail and sizeMultiplier.
Often in UIs it's desirable to try to track the overall status of a request, including the thumbnails. For example, you might want to load an image, start an animation if the asynchronous image load succeeds and perform some fallback action if it fails. If you're using a single primary request, RequestListener will work for this. However, if you then decide to try to make things more performant by adding a thumbnail (or multiple thumbnails), RequestListener is awkward because either you only add it to the main request and it's not called when the thumbnails complete (which defeats the purpose) or it's called for every request and it's hard to keep track of when the overall request has failed. A better option than using RequestListener to track the state of the UI then is to use Target instead. onResourceReady will be called when any thumbnail finishes, which you can use to trigger your animation starting. onLoadFailed will only be called if every request in the chain, including the primary request, fails, which you can use to trigger your fallback behavior. Be sure to pick an appropriate Target subclass when possible, like or when loading into ImageView or com.bumptech.glide.request.target.CustomTarget when using custom rendering. Don't forget to call super()
in the ImageViewTarget
s.
It's best to create a single instance of an exception handler per type of request (usually activity/fragment) rather than pass one in per request to avoid some redundant object allocation.
Return
This request builder.
Parameters
The request listener to use. If null
, this method is a noop.