7

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 Task but the app doesn't return to the previous activity. Any ideas? This is an extract from my code

private class MyTask extends AsyncTask<Void, Void, Void> implements OnDismissListener{
    private boolean exception= false;

    @Override
    protected void onPreExecute(){
        pd = ProgressDialog.show(
                IscrizioniActivity.this,
                "Please wait...",
                "Loading the data",
                true,
                true,
                new DialogInterface.OnCancelListener(){
                    public void onCancel(DialogInterface dialog) {
                        MyTask.this.cancel(true);
                    }
                }
        );
    }

    @Override
    protected Void doInBackground(Void... voids) {
        //do something
        return (null);
    }

    @Override
    protected void onPostExecute(Void voids) {
        pd.dismiss();
        //do something

    }

    public void onDismiss(DialogInterface dialog) {

        this.cancel(true);
    }

}

Regards.

lugeno
  • 1,055
  • 3
  • 10
  • 18
  • this answer may help -> http://stackoverflow.com/questions/7993023/stop-a-thread-downloading-images-when-activity-finishes/7993176#7993176 – Yilmaz Guleryuz Nov 21 '11 at 08:32

4 Answers4

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

 pd is your progressdialog box

and now use cancelListner

    OnCancelListener cancelListener=new OnCancelListener(){
    @Override
    public void onCancel(DialogInterface arg0){
        bCancelled=true;
        finish();
    }
};
Raghav Chopra
  • 527
  • 1
  • 10
  • 26
2

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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
         MyTask.cancel();
      IscrizioniActivity.this.finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
user370305
  • 108,599
  • 23
  • 164
  • 151
  • What do you mean with MyTask.stop? No such method. – lugeno Nov 21 '11 at 08:38
  • Now the problem is on YourCurrentActivity.finish(). Cannot make a static reference to the non-static method finish() from the type Activity – lugeno Nov 21 '11 at 08:49
  • I know that. I had to put this before finis(). Now I'll check if it works. – lugeno Nov 21 '11 at 08:52
  • Tried it. It stops the ecexution (like before) but doesn't return to previous activity. I have to push the back button again. – lugeno Nov 21 '11 at 08:55
  • I call the Activity through an Intent – lugeno Nov 21 '11 at 09:01
  • put there startActivityForResult() instead of startActivity() and override onActivityResult() in that activity. then these works. – user370305 Nov 21 '11 at 09:08
  • I've solved my problem. I've explained my code in the comment to Raghav Chopra's answer. As soon as I'll be able to answer my own question I'll copy the full code. For posterity :) Thanks to all of you! – lugeno Nov 21 '11 at 09:17
1

Try this one...

progress_dialog.setCancelable(true);
progress_dialog.setOnCancelListener(new OnCancelListener() {

    @Override
    public void onCancel(DialogInterface dialog) {
        // TODO Auto-generated method stub
        YourActivity.this.finish();
        }
    });
1

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

public void onDismiss(DialogInterface dialog) {
    this.cancel(true);
    IscrizioniActivity.this.finish();
}
kaspermoerch
  • 16,127
  • 4
  • 44
  • 67