Skip to content

Commit

Permalink
Allow host app to provide a way to clear all resources onStop() (#5145)
Browse files Browse the repository at this point in the history
* * 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()
  • Loading branch information
osamaaftab authored Jul 1, 2023
1 parent 993e59d commit 3d6d038
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions library/src/main/java/com/bumptech/glide/RequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public void run() {

private boolean pauseAllRequestsOnTrimMemoryModerate;

private boolean clearOnStop;

public RequestManager(
@NonNull Glide glide,
@NonNull Lifecycle lifecycle,
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -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();
}
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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) {}

Expand Down

0 comments on commit 3d6d038

Please sign in to comment.