Android AsynTask with progress dialog cancel
Asked Answered
T

4

17

In my android app I use AsynTask with Progress Dialog (Please wait login in ...) for logining user with my web page (web service function inside AsynTask)

I want to dismiss Progress Dialog and cancel AsynTask when user click on Back button on device.

I can't find that kind of example, for interrupting AsynTask. I read abouth cancel(boolean) but I don't know how to call from UI.

Can anyone give me idea.

Thanks

Thrush answered 1/4, 2011 at 13:12 Comment(0)
S
9
public MyActivity extends Activity {


  private MyAsyncTask task;

  public onCreate() {
     task = new MyAsyncTask(); // MyAsyncTask has a progress dialog and dismiss it
     // in an overrided cancel() method 
     task.execute();
  }

  private void handleOnBackButton() {
     task.cancel(true);
  }

Then all you need is to call handleOnBackButton() when user presses back or home. You can do it using onKeyDown() method.

Susquehanna answered 1/4, 2011 at 13:16 Comment(3)
In this case, I think this is better solution than mine. Deleting my answer.Carrie
This will not handle a cancel on the dialog which can happen when the user clicks somewhere else on the screen while it is showing. It seems the answer by sharic19 is more logical as it sets the OnCancelListener on the dialog itselfWithy
You dont need the handleOnBackButton(); its enough to set progressDialog.setCancelable(true); for to make it react on the Back button.Sheba
J
20
    ProgressDialog progressDialog = ProgressDialog.show(ActivityName.this,
            "Title",
            "Message");
    progressDialog.setCancelable(true);
    progressDialog.setOnCancelListener(new OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            // TODO Auto-generated method stub
            // Do something...
        }
    });

The setCancelable(true) method sets whether the dialog is cancelable with the BACK key. You can execute finishing codes through setOnCancelListener -> onCancel method.

Jochbed answered 24/3, 2013 at 16:42 Comment(0)
S
9
public MyActivity extends Activity {


  private MyAsyncTask task;

  public onCreate() {
     task = new MyAsyncTask(); // MyAsyncTask has a progress dialog and dismiss it
     // in an overrided cancel() method 
     task.execute();
  }

  private void handleOnBackButton() {
     task.cancel(true);
  }

Then all you need is to call handleOnBackButton() when user presses back or home. You can do it using onKeyDown() method.

Susquehanna answered 1/4, 2011 at 13:16 Comment(3)
In this case, I think this is better solution than mine. Deleting my answer.Carrie
This will not handle a cancel on the dialog which can happen when the user clicks somewhere else on the screen while it is showing. It seems the answer by sharic19 is more logical as it sets the OnCancelListener on the dialog itselfWithy
You dont need the handleOnBackButton(); its enough to set progressDialog.setCancelable(true); for to make it react on the Back button.Sheba
M
5

You just have to set your ProgressDialog cancelable. And it will disappear when you click "Back" button. Like This :

dialog.setCancelable(true);

You have to override onBackPressed to dismiss the ProgressDialog as well as cancel AsyncTask

@Override
public void onBackPressed() {
YourAsyncTaskObject.cancel(true);
YourProgressDialog.dismiss();
return;
}
Mammoth answered 1/4, 2011 at 13:15 Comment(0)
E
1

I have found that the back button event is consumed the ProgressDialog that is 'show'n, so the Activity does not get to act on the cancel. I had to add a listener to the dialog:

mProgress = ProgressDialog
        .show(this, getText(R.string.progress_title),
        getText(R.string.progressing), true, true,
        new OnCancelListener() {
            public void onCancel(DialogInterface pd) {
                handleOnBackButton();
            }
        });         

Nevermind.

I found it much cleaner to use the Activity's dialog management and add an onDismissListener during onCreateDialog. The dismiss listener can cancel the task. No need to hold a reference to the dialog and that is the only place we need to listen for user cancel.

Equine answered 25/5, 2012 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.