Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Cancel Async Task

I use an async task to upload an image and get some results.

While uploading the image I see a progress dialog, written in onPreExecute() method like this:

    protected void onPreExecute() {           uploadingDialog = new ProgressDialog(MyActivity.this);           uploadingDialog.setMessage("uploading");           uploadingDialog.setCancelable(true);          uploadingDialog.show();     } 

Ok when I press the back button, obviously the dialog disappears because of the setCancelable(true).

But (obviously) the async task doesn't stop.

So how can I fix this? I want to cancel both dialog and async task when I press the back button. Any ideas?

EDIT: FOUND THE SOLUTION. SEE MY ANSWER BELOW.

like image 881
steliosf Avatar asked May 18 '11 02:05

steliosf


People also ask

How do I cancel Async tasks?

You can cancel an asynchronous operation after a period of time by using the CancellationTokenSource. CancelAfter method if you don't want to wait for the operation to finish.

How do I stop AsyncTask when activity is destroyed?

You can either cancel the AsyncTask in the onStop method of your activity or you can let your async task finish, and not loose its progress and relink it to the next instance of your activity.

What is Async task in Android?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

What can I use instead of AsyncTask?

Alternative 1: Using Executor and Handler The executor will help in performing any task in the background and the handler will help to make UI changes.


2 Answers

From SDK:

Cancelling a task

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true.

After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns.

To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

So your code is right for dialog listener:

uploadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {     public void onCancel(DialogInterface dialog) {         myTask.cancel(true);         //finish();     } }); 

Now, as I have mentioned earlier from SDK, you have to check whether the task is cancelled or not, for that you have to check isCancelled() inside the onPreExecute() method.

For example:

if (isCancelled())      break; else {    // do your work here } 
like image 123
Paresh Mayani Avatar answered Oct 12 '22 23:10

Paresh Mayani


FOUND THE SOLUTION: I added an action listener before uploadingDialog.show() like this:

    uploadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){           public void onCancel(DialogInterface dialog) {               myTask.cancel(true);               //finish();           }     }); 

That way when I press the back button, the above OnCancelListener cancels both dialog and task. Also you can add finish() if you want to finish the whole activity on back pressed. Remember to declare your async task as a variable like this:

    MyAsyncTask myTask=null; 

and execute your async task like this:

    myTask = new MyAsyncTask();     myTask.execute(); 
like image 30
steliosf Avatar answered Oct 12 '22 23:10

steliosf