我使用ProgressDialog在线程运行时显示微调器。当线程完成时,该对话框会从视图中移除,并将另一个活动推送到堆栈上。问题是,该对话框未被处理。当我返回到活动(弹出堆栈)并返回到前一个活动时,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())。但是,我不认为这是对内存的良好使用。我可以在内存中挂起一些对话框。如何确保对象已被释放?
Thx
发布于 2011-09-19 09:00:51
创建一个嵌套的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.
}
} 发布于 2012-04-05 16:44:54
解决方案是使用removeDialog(id)而不是dismissDialog(id)。
https://stackoverflow.com/questions/7465144
复制相似问题