Interface RequestListener<R>
- Type Parameters:
R- The type of resource being loaded.
- All Known Implementing Classes:
ExperimentalRequestListener,RequestFutureTarget
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.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanonLoadFailed(GlideException e, Object model, Target<R> target, boolean isFirstResource) Called when an exception occurs during a load, immediately beforeTarget.onLoadFailed(Drawable).booleanonResourceReady(R resource, Object model, Target<R> target, DataSource dataSource, boolean isFirstResource) Called when a load completes successfully, immediately beforeTarget.onResourceReady(Object, com.bumptech.glide.request.transition.Transition).
-
Method Details
-
onLoadFailed
boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<R> target, boolean isFirstResource) Called when an exception occurs during a load, immediately beforeTarget.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 toGlide.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
Targetat this point, as long as you returntruefrom the method to preventTarget.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. }- Parameters:
e- The maybenullexception containing information about why the request failed.model- The model we were trying to load when the exception occurred.target- TheTargetwe were trying to load the image into.isFirstResource-trueif this exception is for the first resource to load.- Returns:
trueto preventTarget.onLoadFailed(Drawable)from being called ontarget, typically because the listener wants to update thetargetor the object thetargetwraps itself orfalseto allowTarget.onLoadFailed(Drawable)to be called ontarget.
-
onResourceReady
boolean onResourceReady(@NonNull R resource, @NonNull Object model, Target<R> target, @NonNull DataSource dataSource, boolean isFirstResource) Called when a load completes successfully, immediately beforeTarget.onResourceReady(Object, com.bumptech.glide.request.transition.Transition).For threading guarantees, see the class comment.
- Parameters:
resource- The resource that was loaded for the target. Non-null because a null resource will result in a call toonLoadFailed(GlideException, Object, Target, boolean)instead of this method.model- The specific model that was used to load the image. Non-null because a null model will result in a call toonLoadFailed(GlideException, Object, Target, boolean)instead of this method.target- The target the model was loaded into.dataSource- TheDataSourcethe resource was loaded from.isFirstResource-trueif 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 betruefor the first image to load andfalsefor the second.- Returns:
trueto preventTarget.onResourceReady(Object, Transition)from being called ontarget, typically because the listener wants to update thetargetor the object thetargetwraps itself orfalseto allowTarget.onResourceReady(Object, Transition)to be called ontarget.
-