Class RequestManager
- java.lang.Object
-
- com.bumptech.glide.RequestManager
-
- All Implemented Interfaces:
android.content.ComponentCallbacks,android.content.ComponentCallbacks2,LifecycleListener
public class RequestManager extends java.lang.Object implements android.content.ComponentCallbacks2, LifecycleListener
A class for managing and starting requests for Glide. Can use activity, fragment and connectivity lifecycle events to intelligently stop, start, and restart requests. Retrieve either by instantiating a new object, or to take advantage built in Activity and Fragment lifecycle handling, use the static Glide.load methods with your Fragment or Activity.
-
-
Constructor Summary
Constructors Constructor Description RequestManager(Glide glide, Lifecycle lifecycle, RequestManagerTreeNode treeNode, android.content.Context context)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description RequestManageraddDefaultRequestListener(RequestListener<java.lang.Object> requestListener)Adds a defaultRequestListenerthat will be added to every request started with thisRequestManager.RequestManagerapplyDefaultRequestOptions(RequestOptions requestOptions)Updates the defaultRequestOptionsfor all loads started with this request manager with the givenRequestOptions.<ResourceType>
RequestBuilder<ResourceType>as(java.lang.Class<ResourceType> resourceClass)Attempts to load the resource using any registeredResourceDecoders that can decode the given resource class or any subclass of the given resource class.RequestBuilder<android.graphics.Bitmap>asBitmap()Attempts to always load the resource as aBitmap, even if it could actually be animated.RequestBuilder<android.graphics.drawable.Drawable>asDrawable()Attempts to always load the resource using any registeredResourceDecoders that can decode any subclass ofDrawable.RequestBuilder<java.io.File>asFile()Attempts to always load aFilecontaining the resource, either using a file path obtained from the media store (for local images/videos), or using Glide's disk cache (for remote images/videos).RequestBuilder<GifDrawable>asGif()Attempts to always load the resource as aGifDrawable.voidclear(android.view.View view)Cancel any pending loads Glide may have for the view and free any resources that may have been loaded for the view.voidclear(Target<?> target)Cancel any pending loads Glide may have for the target and free any resources (such asBitmaps) that may have been loaded for the target so they may be reused.RequestBuilder<java.io.File>download(java.lang.Object model)A helper method equivalent to callingdownloadOnly()()} and thenRequestBuilder.load(Object)with the given model.RequestBuilder<java.io.File>downloadOnly()Attempts always load the resource into the cache and return theFilecontaining the cached source data.booleanisPaused()Returns true if loads for thisRequestManagerare currently paused.RequestBuilder<android.graphics.drawable.Drawable>load(byte[] model)Equivalent to callingasDrawable()and thenRequestBuilder.load(byte[]).RequestBuilder<android.graphics.drawable.Drawable>load(android.graphics.Bitmap bitmap)Equivalent to callingasDrawable()and thenRequestBuilder.load(Bitmap).RequestBuilder<android.graphics.drawable.Drawable>load(android.graphics.drawable.Drawable drawable)Equivalent to callingasDrawable()and thenRequestBuilder.load(Drawable).RequestBuilder<android.graphics.drawable.Drawable>load(android.net.Uri uri)Equivalent to callingasDrawable()and thenRequestBuilder.load(Uri).RequestBuilder<android.graphics.drawable.Drawable>load(java.io.File file)Equivalent to callingasDrawable()and thenRequestBuilder.load(File).RequestBuilder<android.graphics.drawable.Drawable>load(java.lang.Integer resourceId)Equivalent to callingasDrawable()and thenRequestBuilder.load(Integer).RequestBuilder<android.graphics.drawable.Drawable>load(java.lang.Object model)A helper method equivalent to callingasDrawable()and thenRequestBuilder.load(Object)with the given model.RequestBuilder<android.graphics.drawable.Drawable>load(java.lang.String string)Equivalent to callingasDrawable()and thenRequestBuilder.load(String).RequestBuilder<android.graphics.drawable.Drawable>load(java.net.URL url)Deprecated.voidonConfigurationChanged(android.content.res.Configuration newConfig)voidonDestroy()Lifecycle callback that cancels all in progress requests and clears and recycles resources for all completed requests.voidonLowMemory()voidonStart()Lifecycle callback that registers for connectivity events (if the android.permission.ACCESS_NETWORK_STATE permission is present) and restarts failed or paused requests.voidonStop()Lifecycle callback that unregisters for connectivity events (if the android.permission.ACCESS_NETWORK_STATE permission is present) and pauses in progress loads.voidonTrimMemory(int level)voidpauseAllRequests()Cancels any in progress loads and clears resources of completed loads.voidpauseAllRequestsRecursive()PerformspauseAllRequests()recursively for all managers that are contextually descendant to this manager based on the Activity/Fragment hierarchy.voidpauseRequests()Cancels any in progress loads, but does not clear resources of completed loads.voidpauseRequestsRecursive()PerformspauseRequests()recursively for all managers that are contextually descendant to this manager based on the Activity/Fragment hierarchy: When pausing on an Activity all attached fragments will also get paused.voidresumeRequests()Restarts any loads that have not yet completed.voidresumeRequestsRecursive()PerformsresumeRequests()recursively for all managers that are contextually descendant to this manager based on the Activity/Fragment hierarchy.RequestManagersetDefaultRequestOptions(RequestOptions requestOptions)Replaces the defaultRequestOptionsfor all loads started with this request manager with the givenRequestOptions.voidsetPauseAllRequestsOnTrimMemoryModerate(boolean pauseAllOnTrim)Iftruethen clear all in-progress and completed requests when the platform sendsonTrimMemorywith level =TRIM_MEMORY_MODERATE.protected voidsetRequestOptions(RequestOptions toSet)java.lang.StringtoString()
-
-
-
Field Detail
-
glide
protected final Glide glide
-
context
protected final android.content.Context context
-
-
Constructor Detail
-
RequestManager
public RequestManager(@NonNull Glide glide, @NonNull Lifecycle lifecycle, @NonNull RequestManagerTreeNode treeNode, @NonNull android.content.Context context)
-
-
Method Detail
-
setRequestOptions
protected void setRequestOptions(@NonNull RequestOptions toSet)
-
applyDefaultRequestOptions
@NonNull public RequestManager applyDefaultRequestOptions(@NonNull RequestOptions requestOptions)
Updates the defaultRequestOptionsfor all loads started with this request manager with the givenRequestOptions.The
RequestOptionsprovided here are applied on top of those provided viaGlideBuilder.setDefaultRequestOptions(RequestOptions). If there are conflicts, the options applied here will win. Note that this method does not mutate options provided toGlideBuilder.setDefaultRequestOptions(RequestOptions).Multiple sets of options can be applied. If there are conflicts the last
RequestOptionsapplied will win.The modified options will only be applied to loads started after this method is called.
- Returns:
- This request manager.
- See Also:
RequestBuilder.apply(BaseRequestOptions)
-
setDefaultRequestOptions
@NonNull public RequestManager setDefaultRequestOptions(@NonNull RequestOptions requestOptions)
Replaces the defaultRequestOptionsfor all loads started with this request manager with the givenRequestOptions.The
RequestOptionsprovided here replace those that have been previously provided via this method,GlideBuilder.setDefaultRequestOptions(RequestOptions), andapplyDefaultRequestOptions(RequestOptions).Subsequent calls to
applyDefaultRequestOptions(RequestOptions)will not mutate theRequestOptionsprovided here. Instead the manager will create a clone of these options and mutate the clone.- Returns:
- This request manager.
- See Also:
applyDefaultRequestOptions(RequestOptions)
-
addDefaultRequestListener
public RequestManager addDefaultRequestListener(RequestListener<java.lang.Object> requestListener)
Adds a defaultRequestListenerthat will be added to every request started with thisRequestManager.Multiple
RequestListeners can be added here, inRequestManagerscopes or to individualRequestBuilders.RequestListeners are called in the order they're added. Even if an earlierRequestListenerreturnstruefromRequestListener.onLoadFailed(GlideException, Object, Target, boolean)orRequestListener.onResourceReady(Object, Object, Target, DataSource, boolean), it will not prevent subsequentRequestListeners from being called.Because Glide requests can be started for any number of individual resource types, any listener added here has to accept any generic resource type in
RequestListener.onResourceReady(Object, Object, Target, DataSource, boolean). If you must base the behavior of the listener on the resource type, you will need to useinstanceofto do so. It's not safe to cast resource types without first checking withinstanceof.
-
setPauseAllRequestsOnTrimMemoryModerate
public void setPauseAllRequestsOnTrimMemoryModerate(boolean pauseAllOnTrim)
Iftruethen clear all in-progress and completed requests when the platform sendsonTrimMemorywith level =TRIM_MEMORY_MODERATE.
-
isPaused
public boolean isPaused()
Returns true if loads for thisRequestManagerare currently paused.- See Also:
pauseRequests(),resumeRequests()
-
pauseRequests
public void pauseRequests()
Cancels any in progress loads, but does not clear resources of completed loads.Note #
resumeRequests()must be called for any requests made before or while the manager is paused to complete. RequestManagers attached to Fragments and Activities automatically resume onStart().- See Also:
isPaused(),resumeRequests()
-
pauseAllRequests
public void pauseAllRequests()
Cancels any in progress loads and clears resources of completed loads.Note #
resumeRequests()must be called for any requests made before or while the manager is paused to complete. RequestManagers attached to Fragments and Activities automatically resume onStart().This will release the memory used by completed bitmaps but leaves them in any configured caches. When an #
Activityreceives #Activity.onTrimMemory(int)at a level of #ComponentCallbacks2.TRIM_MEMORY_BACKGROUNDthis is desirable in order to keep your process alive longer.- See Also:
isPaused(),resumeRequests()
-
pauseAllRequestsRecursive
public void pauseAllRequestsRecursive()
PerformspauseAllRequests()recursively for all managers that are contextually descendant to this manager based on the Activity/Fragment hierarchy.Similar to
pauseRequestsRecursive()with the exception that it also clears resources of completed loads.
-
pauseRequestsRecursive
public void pauseRequestsRecursive()
PerformspauseRequests()recursively for all managers that are contextually descendant to this manager based on the Activity/Fragment hierarchy:- When pausing on an Activity all attached fragments will also get paused.
- When pausing on an attached Fragment all descendant fragments will also get paused.
- When pausing on a detached Fragment or the application context only the current RequestManager is paused.
Note, on pre-Jelly Bean MR1 calling pause on a Fragment will not cause child fragments to pause, in this case either call pause on the Activity or use a support Fragment.
-
resumeRequests
public void resumeRequests()
Restarts any loads that have not yet completed.- See Also:
isPaused(),pauseRequests()
-
resumeRequestsRecursive
public void resumeRequestsRecursive()
PerformsresumeRequests()recursively for all managers that are contextually descendant to this manager based on the Activity/Fragment hierarchy. The hierarchical semantics are identical as forpauseRequestsRecursive().
-
onStart
public void onStart()
Lifecycle callback that registers for connectivity events (if the android.permission.ACCESS_NETWORK_STATE permission is present) and restarts failed or paused requests.- Specified by:
onStartin interfaceLifecycleListener
-
onStop
public void onStop()
Lifecycle callback that unregisters for connectivity events (if the android.permission.ACCESS_NETWORK_STATE permission is present) and pauses in progress loads.- Specified by:
onStopin interfaceLifecycleListener
-
onDestroy
public void onDestroy()
Lifecycle callback that cancels all in progress requests and clears and recycles resources for all completed requests.- Specified by:
onDestroyin interfaceLifecycleListener
-
asBitmap
@NonNull @CheckResult public RequestBuilder<android.graphics.Bitmap> asBitmap()
Attempts to always load the resource as aBitmap, even if it could actually be animated.- Returns:
- A new request builder for loading a
Bitmap
-
asGif
@NonNull @CheckResult public RequestBuilder<GifDrawable> asGif()
Attempts to always load the resource as aGifDrawable.If the underlying data is not a GIF, this will fail. As a result, this should only be used if the model represents an animated GIF and the caller wants to interact with the GifDrawable directly. Normally using just
asDrawable()is sufficient because it will determine whether or not the given data represents an animated GIF and return the appropriateDrawable, animated or not, automatically.- Returns:
- A new request builder for loading a
GifDrawable.
-
asDrawable
@NonNull @CheckResult public RequestBuilder<android.graphics.drawable.Drawable> asDrawable()
Attempts to always load the resource using any registeredResourceDecoders that can decode any subclass ofDrawable.By default, may return either a
BitmapDrawableorGifDrawable, but if additional decoders are registered for otherDrawablesubclasses, any of those subclasses may also be returned.- Returns:
- A new request builder for loading a
Drawable.
-
load
@NonNull @CheckResult public RequestBuilder<android.graphics.drawable.Drawable> load(@Nullable android.graphics.Bitmap bitmap)
Equivalent to callingasDrawable()and thenRequestBuilder.load(Bitmap).- Returns:
- A new request builder for loading a
Drawableusing the given model.
-
load
@NonNull @CheckResult public RequestBuilder<android.graphics.drawable.Drawable> load(@Nullable android.graphics.drawable.Drawable drawable)
Equivalent to callingasDrawable()and thenRequestBuilder.load(Drawable).- Returns:
- A new request builder for loading a
Drawableusing the given model.
-
load
@NonNull @CheckResult public RequestBuilder<android.graphics.drawable.Drawable> load(@Nullable java.lang.String string)
Equivalent to callingasDrawable()and thenRequestBuilder.load(String).- Returns:
- A new request builder for loading a
Drawableusing the given model.
-
load
@NonNull @CheckResult public RequestBuilder<android.graphics.drawable.Drawable> load(@Nullable android.net.Uri uri)
Equivalent to callingasDrawable()and thenRequestBuilder.load(Uri).- Returns:
- A new request builder for loading a
Drawableusing the given model.
-
load
@NonNull @CheckResult public RequestBuilder<android.graphics.drawable.Drawable> load(@Nullable java.io.File file)
Equivalent to callingasDrawable()and thenRequestBuilder.load(File).- Returns:
- A new request builder for loading a
Drawableusing the given model.
-
load
@NonNull @CheckResult public RequestBuilder<android.graphics.drawable.Drawable> load(@RawRes @DrawableRes @Nullable java.lang.Integer resourceId)
Equivalent to callingasDrawable()and thenRequestBuilder.load(Integer).- Returns:
- A new request builder for loading a
Drawableusing the given model.
-
load
@CheckResult @Deprecated public RequestBuilder<android.graphics.drawable.Drawable> load(@Nullable java.net.URL url)
Deprecated.Equivalent to callingasDrawable()and thenRequestBuilder.load(URL).- Returns:
- A new request builder for loading a
Drawableusing the given model.
-
load
@NonNull @CheckResult public RequestBuilder<android.graphics.drawable.Drawable> load(@Nullable byte[] model)
Equivalent to callingasDrawable()and thenRequestBuilder.load(byte[]).- Returns:
- A new request builder for loading a
Drawableusing the given model.
-
load
@NonNull @CheckResult public RequestBuilder<android.graphics.drawable.Drawable> load(@Nullable java.lang.Object model)
A helper method equivalent to callingasDrawable()and thenRequestBuilder.load(Object)with the given model.- Returns:
- A new request builder for loading a
Drawableusing the given model.
-
downloadOnly
@NonNull @CheckResult public RequestBuilder<java.io.File> downloadOnly()
Attempts always load the resource into the cache and return theFilecontaining the cached source data.This method is designed to work for remote data that is or will be cached using
DiskCacheStrategy.DATA. As a result, specifying aDiskCacheStrategyon this request is generally not recommended.- Returns:
- A new request builder for downloading content to cache and returning the cache File.
-
download
@NonNull @CheckResult public RequestBuilder<java.io.File> download(@Nullable java.lang.Object model)
A helper method equivalent to callingdownloadOnly()()} and thenRequestBuilder.load(Object)with the given model.- Returns:
- A new request builder for loading a
Drawableusing the given model.
-
asFile
@NonNull @CheckResult public RequestBuilder<java.io.File> asFile()
Attempts to always load aFilecontaining the resource, either using a file path obtained from the media store (for local images/videos), or using Glide's disk cache (for remote images/videos).For remote content, prefer
downloadOnly().- Returns:
- A new request builder for obtaining File paths to content.
-
as
@NonNull @CheckResult public <ResourceType> RequestBuilder<ResourceType> as(@NonNull java.lang.Class<ResourceType> resourceClass)
Attempts to load the resource using any registeredResourceDecoders that can decode the given resource class or any subclass of the given resource class.- Parameters:
resourceClass- The resource to decode.- Returns:
- A new request builder for loading the given resource class.
-
clear
public void clear(@NonNull android.view.View view)Cancel any pending loads Glide may have for the view and free any resources that may have been loaded for the view.Note that this will only work if
View.setTag(Object)is not called on this view outside of Glide.- Parameters:
view- The view to cancel loads and free resources for.- Throws:
java.lang.IllegalArgumentException- if an object other than Glide's metadata is put as the view's tag.- See Also:
clear(Target)
-
clear
public void clear(@Nullable Target<?> target)Cancel any pending loads Glide may have for the target and free any resources (such asBitmaps) that may have been loaded for the target so they may be reused.- Parameters:
target- The Target to cancel loads for.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
onTrimMemory
public void onTrimMemory(int level)
- Specified by:
onTrimMemoryin interfaceandroid.content.ComponentCallbacks2
-
onLowMemory
public void onLowMemory()
- Specified by:
onLowMemoryin interfaceandroid.content.ComponentCallbacks
-
onConfigurationChanged
public void onConfigurationChanged(android.content.res.Configuration newConfig)
- Specified by:
onConfigurationChangedin interfaceandroid.content.ComponentCallbacks
-
-