From 3d6d038f7341dc71872d076b42d74492fec197b9 Mon Sep 17 00:00:00 2001 From: Osama Aftab Date: Sat, 1 Jul 2023 19:27:48 +0100 Subject: [PATCH] Allow host app to provide a way to clear all resources onStop() (#5145) * * Integrated a way in glide to allow host app to provide a way to clear resources onStop(). * * Clear targetTracker only in case clearOnStop is true * * Extracted a clearRequests() and re-used it in onStop() and onDestroy() --- .../com/bumptech/glide/RequestManager.java | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/bumptech/glide/RequestManager.java b/library/src/main/java/com/bumptech/glide/RequestManager.java index ba9b33ef11..5b3ed2c3fd 100644 --- a/library/src/main/java/com/bumptech/glide/RequestManager.java +++ b/library/src/main/java/com/bumptech/glide/RequestManager.java @@ -95,6 +95,8 @@ public void run() { private boolean pauseAllRequestsOnTrimMemoryModerate; + private boolean clearOnStop; + public RequestManager( @NonNull Glide glide, @NonNull Lifecycle lifecycle, @@ -203,6 +205,17 @@ public synchronized RequestManager setDefaultRequestOptions( return this; } + /** + * Clear all resources when onStop() from {@link LifecycleListener} is called. + * + * @return This request manager. + */ + @NonNull + public synchronized RequestManager clearOnStop() { + clearOnStop = true; + return this; + } + /** * Adds a default {@link RequestListener} that will be added to every request started with this * {@link RequestManager}. @@ -354,12 +367,17 @@ public synchronized void onStart() { /** * Lifecycle callback that unregisters for connectivity events (if the - * android.permission.ACCESS_NETWORK_STATE permission is present) and pauses in progress loads. + * android.permission.ACCESS_NETWORK_STATE permission is present) and pauses in progress loads + * and clears all resources if {@link #clearOnStop()} is called. */ @Override public synchronized void onStop() { - pauseRequests(); targetTracker.onStop(); + if (clearOnStop) { + clearRequests(); + } else { + pauseRequests(); + } } /** @@ -369,10 +387,7 @@ public synchronized void onStop() { @Override public synchronized void onDestroy() { targetTracker.onDestroy(); - for (Target target : targetTracker.getAll()) { - clear(target); - } - targetTracker.clear(); + clearRequests(); requestTracker.clearRequests(); lifecycle.removeListener(this); lifecycle.removeListener(connectivityMonitor); @@ -703,6 +718,13 @@ public void onLowMemory() { // Nothing to add conditionally. See Glide#onTrimMemory for unconditional behavior. } + private synchronized void clearRequests() { + for (Target target : targetTracker.getAll()) { + clear(target); + } + targetTracker.clear(); + } + @Override public void onConfigurationChanged(Configuration newConfig) {}