简体   繁体   English

取消Android asyncTask进度对话框

[英]Cancel Android asyncTask progress Dialog

I have an android application that employs AsyncTasks with progress dialogs. 我有一个Android应用程序,它使用带有进度对话框的AsyncTasks
My problem is that although i have managed to cancel the AsynchTasks i get 我的问题是,虽然我已经设法取消了我得到的AsynchTasks

Activity ... has leaked window 

Its because even though i have managed to cancel the asynchTask i havent cancelled the progress dialog. 因为即使我已经设法取消asynchTask我还没有取消进度对话框。
how can i guarantee to cancel the asynctasks progress dialog? 我怎样才能保证取消asynctasks进度对话框?

@SuppressWarnings("rawtypes")
public OnCancelListener buildOnCancelListener(final AsyncTask asyncTask) {
return new DialogInterface.OnCancelListener() {
@Override
public void onCancel(final DialogInterface dialogInterface) {
    asyncTask.cancel(true);
    }
};
}

Put yourDialog.dismiss(); yourDialog.dismiss(); in the onCancelled method and in onPostExecute . onCancelled方法和onPostExecute If you are using AsyncTask.cancel(...) the onCancelled() will be invoked after doInBackground() NOT onPostExecute(). 如果您正在使用AsyncTask.cancel(...),则在doInBackground()之后将调用onCancelled()而不是onPostExecute()。

This is documented here . 在此处记录

This is because you're trying to move to a new Activity without dismissing the Dialog first. 这是因为您尝试移动到新的Activity而不首先解除Dialog。

You can try this: 你可以试试这个:

  • use publishProgress() and onProgressUpdate() in AsyncTask to update the ProgressDialog . AsyncTask使用publishProgress()onProgressUpdate()来更新ProgressDialog
  • in onProgressUpdate() tests for the dialog to be visible/alive before trying to update it. onProgressUpdate()测试对话框在尝试更新之前是可见/活动的。 This will not bring any synchronization issue since onProgressUpdate run in the same UI Thread of the activity. 由于onProgressUpdate在活动的同一UI线程中运行,因此不会带来任何同步问题。
  • call dialog.dismiss() both on onCanceled() and onPostExecute() . 调用dialog.dismiss()上都onCanceled()onPostExecute()
  • be sure to call dialog.dismiss() in Activity.onStop() if your dialog is already visible. 如果对话框已经可见,请dialog.dismiss()Activity.onStop()调用dialog.dismiss()

Calling dialog.dismiss() in onCancel() will not guarantee that the problem is solved since 调用dialog.dismiss()onCancel()将不能保证,因为问题解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM