本文介绍了Android的 - 停止的AsyncTask时后退按钮是pressed并返回到previous活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个AsyncTask的,我希望它科技创新政策执行时,后退按钮是pressed。我也希望应用程序返回显示活动的previous。看来我已经停止了任务管理,但是该应用程序不返回到previous活动。有任何想法吗?这是从我的code提取物
私有类MyTask的扩展AsyncTask的<虚空,虚空,虚空>实现OnDismissListener {
私人布尔例外= FALSE;
@覆盖
在preExecute保护无效(){
PD = ProgressDialog.show(
IscrizioniActivity.this,
请稍候...,
加载数据,
真正,
真正,
新DialogInterface.OnCancelListener(){
公共无效OnCancel的(DialogInterface对话){
MyTask.this.cancel(真正的);
}
}
);
}
@覆盖
保护无效doInBackground(虚空......空隙){
//做一点事
返程(空);
}
@覆盖
保护无效onPostExecute(无效空隙){
pd.dismiss();
//做一点事
}
公共无效onDismiss(DialogInterface对话){
this.cancel(真正的);
}
}
问候。
解决方案
pd.setCancelable(真正的);
pd.setOnCancelListener(cancelListener);
bCancelled = FALSE;
PD是你progressdialog箱
和现在使用cancelListner
OnCancelListener cancelListener =新OnCancelListener(){
@覆盖
公共无效OnCancel的(DialogInterface为arg0){
bCancelled = 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.
解决方案
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();
}
};
这篇关于Android的 - 停止的AsyncTask时后退按钮是pressed并返回到previous活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!