我正在使用 ProgressDialog 在线程运行时显示微调器。当线程结束时,对话框从 View 中移除,另一个 Activity 被压入堆栈。问题是,对话框没有被处理掉。当我返回 Activity(弹出堆栈)并返回到前一个 Activity 时,将调用 ProgressDialog 的相同实例(当要求显示对话框时)。
创建对话框:
protected Dialog onCreateDialog(int id) {
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setMessage("Searching for ..." + txtPart.getText().toString());
// Start thread
progThread = new AcmXmlSearchHelper(handler, txtTitle.getText().toString(), txtPart.getText().toString(), txtSection.getText().toString(), this);
progThread.start();
return progDialog;
}
显示对话框:
showDialog(getProgressId());
关闭对话框:
dismissDialog(getProgressId());
简单的解决方案是调用具有唯一 ID 的 ProgressDialog (showDialog())。但是,我不认为这是对内存的良好利用。我可以在内存中挂起许多对话框。如何确保对象已被处置?
谢谢
最佳答案
创建一个嵌套的 AsyncTask 类可能会为您解决问题:
private static class MyTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
// Show dialog here - note: showDialog is depricated.
}
protected Void doInBackground(Void... unused) {
// do the stuff that's in your thread here.
return null;
}
protected void onPostExecute(Void unused) {
// Dismiss dialog here - note: dismissDialog is depricated.
}
}
关于java - 处理 ProgressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7465144/