我有一个像这样实现的ProgressDialog:
// show progress dialog while date is loading progressDialog = ProgressDialog.show(XYActivity.this,getResources().getString(R.string.progress_dialog_please_wait),getResources().getString(R.string.progress_dialog_loading),true); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { cancel(true); Log.w(LOGTAG,"loading cancelled via back button"); } }); progressDialog.setCancelable(true);
此ProgressDialog在AsyncTask(PreExecute)中实现,因此cancel(true)方法停止AsyncTask.一切正常.
问题是,我可以在屏幕上随机触摸取消ProgressDialog.我只想通过按下后退按钮来解除对话框.请帮我!感谢你们.
解决方法
这对我有用:
@Override protected void onPreExecute() { progressDialog = ProgressDialog.show(context,"Title","Loading...",true,new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { cancel(true); } }); progressDialog.setCanceledOnTouchOutside(false); }
GedankenNebel建议的setCanceledOnTouchOutside很干净.