package com.bumptech.glide.manager; import java.util.Collections; import java.util.Set; import java.util.WeakHashMap; /** * A {@link com.bumptech.glide.manager.Lifecycle} implementation for tracking and notifying listeners of * {@link android.app.Fragment} and {@link android.app.Activity} lifecycle events. */ class ActivityFragmentLifecycle implements Lifecycle { private final Set lifecycleListeners = Collections.synchronizedSet(Collections.newSetFromMap(new WeakHashMap())); private boolean isStarted; private boolean isDestroyed; /** * Adds the given listener to the list of listeners to be notified on each lifecycle event. * *

* The latest lifecycle event will be called on the given listener synchronously in this method. If the * activity or fragment is stopped, {@link LifecycleListener#onStop()}} will be called, and same for onStart and * onDestroy. *

* *

* Note - {@link com.bumptech.glide.manager.LifecycleListener}s that are added more than once will have their * lifecycle methods called more than once. It is the caller's responsibility to avoid adding listeners * multiple times. *

*/ @Override public void addListener(LifecycleListener listener) { lifecycleListeners.add(listener); if (isDestroyed) { listener.onDestroy(); } else if (isStarted) { listener.onStart(); } else { listener.onStop(); } } void onStart() { isStarted = true; for (LifecycleListener lifecycleListener : lifecycleListeners) { lifecycleListener.onStart(); } } void onStop() { isStarted = false; for (LifecycleListener lifecycleListener : lifecycleListeners) { lifecycleListener.onStop(); } } void onDestroy() { isDestroyed = true; for (LifecycleListener lifecycleListener : lifecycleListeners) { lifecycleListener.onDestroy(); } } }