Android - 按下后退按钮时停止 AsyncTask 并返回到上一个 Activity

发布于 2024-12-17 07:49:04 字数 1070 浏览 0 评论 0原文

我有一个 AsyncTask,我希望它在按下后退按钮时停止执行。我还希望应用程序返回到之前显示的 Activity。看来我已经成功停止了任务,但应用程序没有返回到之前的活动。有什么想法吗?这是我的代码问候的摘录

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);
    }

}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我们的影子 2024-12-24 07:49:04
pd.setCancelable(true);
    pd.setOnCancelListener(cancelListener);
    bCancelled=false;

 pd is your progressdialog box

现在使用cancelListner

    OnCancelListener cancelListener=new OnCancelListener(){
    @Override
    public void onCancel(DialogInterface arg0){
        bCancelled=true;
        finish();
    }
};
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();
    }
};
我不咬妳我踢妳 2024-12-24 07:49:04

在您的活动中,覆盖后退按钮,停止其中的 AsyncTask,并为当前活动调用完成。

@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);
}

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);
}
若无相欠,怎会相见 2024-12-24 07:49:04

您是否尝试过在 onDismiss() 方法中添加 finish() 调用:

public void onDismiss(DialogInterface dialog) {
    this.cancel(true);
    IscrizioniActivity.this.finish();
}

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

public void onDismiss(DialogInterface dialog) {
    this.cancel(true);
    IscrizioniActivity.this.finish();
}
绅刃 2024-12-24 07:49:04

试试这个...

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

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

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();
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文