Ensure single pipeline instance
@@ -62,6 +62,8 @@ public class ImagePipelineFactory {
private static final Class<?> TAG = ImagePipelineFactory.class;
private static ImagePipelineFactory sInstance = null;
+ private static boolean sForceSinglePipelineInstance;
+ private static ImagePipeline sImagePipeline;
private final ThreadHandoffProducerQueue mThreadHandoffProducerQueue;
/** Gets the instance of {@link ImagePipelineFactory}. */
@@ -90,6 +92,19 @@ public class ImagePipelineFactory {
}
}
+ /** Initializes {@link ImagePipelineFactory} with the specified config. */
+ public static synchronized void initialize(
+ ImagePipelineConfig imagePipelineConfig, boolean forceSinglePipelineInstance) {
+ if (sInstance != null) {
+ FLog.w(
+ TAG,
+ "ImagePipelineFactory has already been initialized! `ImagePipelineFactory.initialize(...)` should only be called once to avoid unexpected behavior.");
+ }
+
+ sForceSinglePipelineInstance = forceSinglePipelineInstance;
+ sInstance = new ImagePipelineFactory(imagePipelineConfig);
+ }
+
/** Initializes {@link ImagePipelineFactory} with the specified config. */
public static synchronized void initialize(ImagePipelineConfig imagePipelineConfig) {
if (sInstance != null) {
@@ -277,9 +292,21 @@ public class ImagePipelineFactory {
}
public ImagePipeline getImagePipeline() {
+ if (sForceSinglePipelineInstance) {
+ if (sImagePipeline == null) {
+ sImagePipeline = createImagePipeline();
+ mImagePipeline = sImagePipeline;
+ }
+ return sImagePipeline;
+ }
if (mImagePipeline == null) {
- mImagePipeline =
- new ImagePipeline(
+ mImagePipeline = createImagePipeline();
+ }
+ return mImagePipeline;
+ }
+
+ private ImagePipeline createImagePipeline() {
+ return new ImagePipeline(
getProducerSequenceFactory(),
mConfig.getRequestListeners(),
mConfig.getRequestListener2s(),
@@ -295,8 +322,6 @@ public class ImagePipelineFactory {
mConfig.getCallerContextVerifier(),
mConfig);
}
- return mImagePipeline;
- }
public PlatformBitmapFactory getPlatformBitmapFactory() {
if (mPlatformBitmapFactory == null) {
