Skip to content Skip to sidebar Skip to footer

Android - Stop Asynctask When Back Button Is Pressed And Return To Previous Activity

I've an AsyncTask and I want it to stip execution when back button is pressed. I also want the app to return to the previous displayed Activity. It seems I've managed in stop the T

Solution 1:

pd.setCancelable(true);
    pd.setOnCancelListener(cancelListener);
    bCancelled=false;

 pd is your progressdialog box

and now use cancelListner

OnCancelListener cancelListener=newOnCancelListener(){
    @OverridepublicvoidonCancel(DialogInterface arg0){
        bCancelled=true;
        finish();
    }
};

Solution 2:

In your activity, override Back Button, stop the AsyncTask in it, and call finish for current activity.

@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
         MyTask.cancel();
      IscrizioniActivity.this.finish();
        returntrue;
    }
    returnsuper.onKeyDown(keyCode, event);
}

Solution 3:

Have you tried adding a finish() call in the onDismiss() method:

publicvoidonDismiss(DialogInterface dialog){
    this.cancel(true);
    IscrizioniActivity.this.finish();
}

Solution 4:

Try this one...

progress_dialog.setCancelable(true);
progress_dialog.setOnCancelListener(newOnCancelListener() {

    @OverridepublicvoidonCancel(DialogInterface dialog) {
        // TODO Auto-generated method stub
        YourActivity.this.finish();
        }
    });

Post a Comment for "Android - Stop Asynctask When Back Button Is Pressed And Return To Previous Activity"