Question

I'm using the Universal Image Loader from nostra13 at https://github.com/nostra13/Android-Universal-Image-Loader. My problem is:

I have these classes

PhotoActivity extends FragmentActivity

ImageFragment extends Fragment

I initialize the imageLoader in the ImageFragment using imageLoader = ImageLoader.getInstance(); and in this fragment, I also display a few images using imageLoader.displayImage(...).

When my PhotoActivity gets stopped (onStop() is called), my ImageFragment stops as well and this should stop any image loading because I call imageLoader.stop() in the onStop() method. However, this is not the case and it gives me NullPointerException because onLoadingComplete() of ImageLoadingListener() is called afterwards, and this method calls getActivity() which is null at this point because the fragment was detached from its activity.

imageLoader.stop() should stop every loading process, no?

Was it helpful?

Solution

ImageLoader use ExecutorService as pool for tasks. ImageLoader.stop() just do ExecutorService.shutdown() which has next Java docs:

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

This method does not wait for previously submitted tasks to complete execution.

There is no guaranty that active tasks will be shutdown immediately after shutdown(). Maybe using of shutdownNow() is better option for that case (I'll think about it), but it still can't give you a guarantee of immediately task shutdown.

So at this moment anyway you should check getActivity(). But I'll think about preventing firing of callbacks after ImageLoader has stopped. Maybe in the next version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top