Skip to content

Commit 09e33a2

Browse files
kurtnsjudd
kurtn
authored andcommitted
Add pauseAllRequests()
pauseAllRequests() can be called when the activity is in the background to reclaim memory used by the active bitmaps. As the caches are unaffected by this, returning to the activity and loading is still fast. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=177660869
1 parent 5d760e2 commit 09e33a2

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

library/src/main/java/com/bumptech/glide/RequestManager.java

+25
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ public boolean isPaused() {
205205
/**
206206
* Cancels any in progress loads, but does not clear resources of completed loads.
207207
*
208+
* <p>Note #{@link #resumeRequests()} must be called for any requests made before or while the
209+
* manager is paused to complete. RequestManagers attached to Fragments and Activities
210+
* automatically resume onStart().
211+
*
208212
* @see #isPaused()
209213
* @see #resumeRequests()
210214
*/
@@ -213,6 +217,27 @@ public void pauseRequests() {
213217
requestTracker.pauseRequests();
214218
}
215219

220+
/**
221+
* Cancels any in progress loads and clears resources of completed loads.
222+
*
223+
* <p>Note #{@link #resumeRequests()} must be called for any requests made before or while the
224+
* manager is paused to complete. RequestManagers attached to Fragments and Activities
225+
* automatically resume onStart().
226+
*
227+
* <p>This will release the memory used by completed bitmaps but leaves them in any configured
228+
* caches. When an #{@link android.app.Activity} receives #{@link
229+
* android.app.Activity#onTrimMemory(int)} at a level of #{@link
230+
* android.content.ComponentCallbacks2#TRIM_MEMORY_BACKGROUND} this is desirable in order to keep
231+
* your process alive longer.
232+
*
233+
* @see #isPaused()
234+
* @see #resumeRequests()
235+
*/
236+
public void pauseAllRequests() {
237+
Util.assertMainThread();
238+
requestTracker.pauseAllRequests();
239+
}
240+
216241
/**
217242
* Performs {@link #pauseRequests()} recursively for all managers that are contextually
218243
* descendant to this manager based on the Activity/Fragment hierarchy:

library/src/main/java/com/bumptech/glide/manager/RequestTracker.java

+11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ public void pauseRequests() {
9797
}
9898
}
9999

100+
/** Stops any in progress requests and releases bitmaps associated with completed requests. */
101+
public void pauseAllRequests() {
102+
isPaused = true;
103+
for (Request request : Util.getSnapshot(requests)) {
104+
if (request.isRunning() || request.isComplete()) {
105+
request.pause();
106+
pendingRequests.add(request);
107+
}
108+
}
109+
}
110+
100111
/**
101112
* Starts any not yet completed or failed requests.
102113
*/

library/src/test/java/com/bumptech/glide/manager/RequestTrackerTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,26 @@ public void testReturnsFalseFromIsPausedWhenResumed() {
354354
assertFalse(tracker.isPaused());
355355
}
356356

357+
@Test
358+
public void testPauseAllRequests_returnsTrueFromIsPaused() {
359+
tracker.pauseAllRequests();
360+
assertTrue(tracker.isPaused());
361+
}
362+
363+
@Test
364+
public void testPauseAllRequests_whenRequestComplete_pausesRequest() {
365+
Request request = mock(Request.class);
366+
when(request.isFailed()).thenReturn(false);
367+
when(request.isComplete()).thenReturn(true);
368+
tracker.addRequest(request);
369+
tracker.pauseAllRequests();
370+
verify(request).pause();
371+
372+
when(request.isComplete()).thenReturn(false);
373+
tracker.resumeRequests();
374+
verify(request).begin();
375+
}
376+
357377
private class ClearAndRemoveRequest implements Answer<Void> {
358378

359379
private final Request toRemove;

0 commit comments

Comments
 (0)