R
- The type of resource being loaded.public interface RequestListener<R>
All methods in this interface will be called from a background thread if the RequestListener
is added to a request that is started with RequestBuilder.submit()
,
RequestBuilder.submit(int, int)
, or RequestBuilder.into(int, int)
. Those methods
no longer post results back to the main thread to avoid the unnecessary thread interactions and
corresponding latency. As a side affect though, listeners added to those requests are no longer
called on the main thread. RequestListeners
added to requests started with RequestBuilder.into(Target)
or RequestBuilder.into(ImageView)
will continue to be called
back on the main thread.
Modifier and Type | Method and Description |
---|---|
boolean |
onLoadFailed(GlideException e,
java.lang.Object model,
Target<R> target,
boolean isFirstResource)
Called when an exception occurs during a load, immediately before
Target.onLoadFailed(Drawable) . |
boolean |
onResourceReady(R resource,
java.lang.Object model,
Target<R> target,
DataSource dataSource,
boolean isFirstResource)
Called when a load completes successfully, immediately before
Target.onResourceReady(Object, com.bumptech.glide.request.transition.Transition) . |
boolean onLoadFailed(@Nullable GlideException e, java.lang.Object model, Target<R> target, boolean isFirstResource)
Target.onLoadFailed(Drawable)
. Will only be called if we currently want to display an image
for the given model in the given target. It is recommended to create a single instance per
activity/fragment rather than instantiate a new object for each call to Glide.with(fragment/activity).load()
to avoid object churn.
It is not safe to reload this or a different model in this callback. If you need to do so
use RequestBuilder.error(RequestBuilder)
instead.
Although you can't start an entirely new load, it is safe to change what is displayed in the
Target
at this point, as long as you return true
from the method to prevent
Target.onLoadFailed(Drawable)
from being called.
For threading guarantees, see the class comment.
For example:
public boolean onLoadFailed(Exception e, T model, Target target, boolean isFirstResource) {
target.setPlaceholder(R.drawable.a_specific_error_for_my_exception);
return true; // Prevent onLoadFailed from being called on the Target.
}
e
- The maybe null
exception containing information about why the request failed.model
- The model we were trying to load when the exception occurred.target
- The Target
we were trying to load the image into.isFirstResource
- true
if this exception is for the first resource to load.true
to prevent Target.onLoadFailed(Drawable)
from being called on
target
, typically because the listener wants to update the target
or the
object the target
wraps itself or false
to allow Target.onLoadFailed(Drawable)
to be called on target
.boolean onResourceReady(R resource, java.lang.Object model, Target<R> target, DataSource dataSource, boolean isFirstResource)
Target.onResourceReady(Object, com.bumptech.glide.request.transition.Transition)
.
For threading guarantees, see the class comment.
resource
- The resource that was loaded for the target.model
- The specific model that was used to load the image.target
- The target the model was loaded into.dataSource
- The DataSource
the resource was loaded from.isFirstResource
- true
if this is the first resource to in this load to be loaded
into the target. For example when loading a thumbnail and a full-sized image, this will be
true
for the first image to load and false
for the second.true
to prevent Target.onLoadFailed(Drawable)
from being called on
target
, typically because the listener wants to update the target
or the
object the target
wraps itself or false
to allow Target.onLoadFailed(Drawable)
to be called on target
.