我有一个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,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); } }
问候.
解决方法
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(); } };