Android - 按下后退按钮时停止 AsyncTask 并返回到上一个 Activity
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
现在使用cancelListner
and now use cancelListner
在您的活动中,覆盖后退按钮,停止其中的 AsyncTask,并为当前活动调用完成。
In your activity, override Back Button, stop the AsyncTask in it, and call finish for current activity.
您是否尝试过在
onDismiss()
方法中添加finish()
调用:Have you tried adding a
finish()
call in theonDismiss()
method:试试这个...
Try this one...