D
D
Danil Ochagov2018-08-04 16:07:42
Java
Danil Ochagov, 2018-08-04 16:07:42

How to continue execution of a stopped AsycnTask in Android?

I work with AsyncTask and it became necessary to pause the download (the download is not real, everything is normal for the sake of the example), there are start, stop, resume buttons. When you click on start, the execute() method is called; , and when clicking on stop, the cancel(false); which makes isCancelled() from false to true and this code:

if (isCancelled()) {
                    Toast.makeText(MainActivity.this, "The load stopped!", Toast.LENGTH_SHORT).show();
                    return null;
                }

Stops the task, how can I continue its execution when I click on the resume button?
While there is such a code on the button
case R.id.b_resume :
                if (myAsyncTaskDemo.isCancelled()) {
                    // resume
                }

Or, if you have already stopped work, then you can not continue?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-08-05
@danilochagov

You can't use asynctasks this way.
1) A canceled task cannot be resumed
2) By default, they are executed sequentially, so even if you can do such a trick, it will stop the rest of the tasks in the application
3) You have a memory leak. Doing so is not recommended.
Take a better real example, and don't fool around. Nobody uses asynctasks for downloads that can be paused (i.e. long enough). There are other APIs for this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question