1

Hi in my app there is an Async Task to read contact detail(which take a little bit time).It does well , but what the problem is when i cancel the Async Task before it completes,fetching details my app get crash.So i was think that make the app to exit when i cancel the Async Task.I search the web and find some method but it did not worked,so how can i exit my app when cancelling an async task? My Asyn Task code(normal code)

public class FetchingContact extends AsyncTask<String, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(
            MobiMailActivity.this);

    // can use UI thread here
    protected void onPreExecute() {
        this.dialog.setMessage("Fetching Contact...");
        this.dialog.show();
    }

    // automatically done on worker thread (separate from UI thread)
    protected Void doInBackground(final String... args) {
        readContact();
        if (isCancelled ()) {

            finish();
        }

        return null;
    }

    // can use UI thread here
    protected void onPostExecute(final Void unused) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
            CharSequence test=sam;
            //  search_sort.setText(test);
                check(test);


        }
        // reset the output view by retrieving the new data
        // (note, this is a naive example, in the real world it might make
        // sense
        // to have a cache of the data and just append to what is already
        // there, or such
        // in order to cut down on expensive database operations)
        // new SelectDataTask().execute();
    }

}
Ramz
  • 7,116
  • 6
  • 63
  • 88
  • no error while loading asy task but when canel that task makes crash its is due the in completion of that task.My question is how to exit app when canel a asyn task – Ramz Aug 20 '12 at 10:49
  • What u r doing to exit the app on cancel is correct it seems but my concern is when u finish the activity, progressDialog is still active. So logcat message shows exact reason for crash – Braj Aug 20 '12 at 10:58
  • 1
    Here is my answer for: [**Android: Cancel Async Task**](http://stackoverflow.com/a/6053943/379693) – Paresh Mayani Aug 20 '12 at 11:21
  • perfect got it post you answer so that i can accept it @Paresh Mayani – Ramz Aug 20 '12 at 12:03
  • @Ramz sorry i can't and i dont want :) (SO Rule) – Paresh Mayani Aug 20 '12 at 12:29
  • Please follow the instructions [here](https://stackoverflow.com/a/7734109/6736510) – Johnyoat Jul 11 '18 at 11:50

2 Answers2

1

A AsyncTask 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.)

This is literally quoted from the AsyncTask. But seeing as you have put your entire code in a method called readContact(), you can't do this.

What you could do is this:

protected void onPostExecute(final Void unused) {
    if (this.dialog.isShowing()) {
        this.dialog.dismiss();
        (if !isCancelled()) {
            CharSequence test=sam;
        //  search_sort.setText(test);
            check(test);
        }
    }
}

So you check at the end of your AsyncTask if something should be done. This is not the way to do it, so I suggest you take your readContact() method, and put it entirely in the AsyncTask, or if it's a method inside your AsyncTask, call isCancelled() check in there.

tolgap
  • 9,629
  • 10
  • 51
  • 65
  • but how i exit from my app on cancel.i think i cancel my app during readContact(); in do in back ground – Ramz Aug 20 '12 at 10:55
0
  1. I assume you try to get the result by calling get() method, that throws exception when the task is cancelled. How about checking isCancelled() before you try to retrieve the result?

    if(!task.isCancelled()) { Result result = task.get(); }

  2. Also, you might try to check out onCancelled(). This runs on the UI thread, and will be called when the task is cancelled.

Mate Gulyas
  • 609
  • 2
  • 8
  • 22