Rafael
Rafael

Reputation: 6369

Get back running threads on returning back to activity in Android

In my app, where I user can start downloading multiple images at once I use threads for downloading process and also show download progress. I am running multiple threads via ThreadPoolExecutor. Problem is when user opens one of the images(activity goes to background) and I lose connection to running threads. How I can get connection to threads back.

public void submitRunnableTask(Runnable task) {
    if(!mPool.isShutdown() && mPool.getActiveCount() != mPool.getMaximumPoolSize()) {
        mPool.submit(task);
    } else {
        new Thread(task).start();
    }
}

Upvotes: 0

Views: 69

Answers (2)

Hazam
Hazam

Reputation: 62

Beyond suggesting great libraries out there (Picasso rocks BTW), I understand that your thread pool is tightly coupled to the lifecycle of the Activity: when the activity dies, so does the thread pool (or better, you loose the reference to it, so even though threads may still be alive, for you it's dead :)

I suggest extracting your ThreadPool to an external class - a plain Java singleton maybe, or better an Android Service - which can stay alive longer than the activity using it.

Upvotes: 1

florent champigny
florent champigny

Reputation: 979

I suggest you to use Picasso for image downloading, it's very simple to use and so effective : (it's one of my favorites libs)

square.github.io/picasso/

Picasso.with(context).load(pictureUrl).into(imageView)

Upvotes: 0

Related Questions