Android:活动暂停时进度对话框不会关闭
我在处理 ProgressDialog 和用户按下 home 键时遇到了一个小问题。
我按如下方式创建 ProgressDialog:
runOnUiThread(new Runnable() {
public void run() {
progressDialog = ProgressDialog.show(this, "",this.getResources().getString( R.string.AProgressMessage), true);
}
});
并在完成互联网下载后将其关闭。
progressDialog.dismiss();
问题是,当用户按下主页键时,有时调用关闭的线程会被杀死,但对话框永远不会被关闭......因此,当应用程序重新启动时,它会卡在 ProgressDialog 后面。
有什么想法吗?
I'm having a small problem handeling ProgressDialog and the suer hitting the home key.
I create my ProgressDialog as follows:
runOnUiThread(new Runnable() {
public void run() {
progressDialog = ProgressDialog.show(this, "",this.getResources().getString( R.string.AProgressMessage), true);
}
});
and dismiss it when I finished downloading stuff of internet.
progressDialog.dismiss();
the problem is when a user hit the home key, sometimes the Thread that calls the dismiss is kille but hte dialog never gets dismissed... therefore when the app relaunches it gets stuck behind a ProgressDialog.
Any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这个帖子已经很老了,但我认为我的回答可以帮助很多人。
Activity 类有一个
onPause
方法。因此,如果您从正在暂停的同一活动调用该对话框,则可以使用onPause
方法来关闭该对话框。将对话框变量设置为全局变量,并定义
onPause
方法,如下所示:I know this thread is quite old, but I think my answer could help many people.
Activity class has an
onPause
method. So if you call the dialog from the same activity that is being paused, you can use theonPause
method to dimiss the dialog.Make the dialog variable global, and define the
onPause
method as follows:为什么线程会被杀死?
如果Android系统认为内存不足,它只会杀死整个进程,而不是单个线程。您确定始终调用progressDialog.dismiss(),即使线程由于未捕获的异常(例如)而停止?
顺便说一句,您可能应该使用 AsyncTask 而不是自己进行线程管理,请参阅此处。
Why would the Thread be killed?
If the Android system thinks that the memory is low, it will only kill whole processes and not individual threads. Are you sure you always call
progressDialog.dismiss()
, even if the thread stops because of an uncaught exception (for example)?By the way, you should probably use AsyncTask instead of doing the thread management yourself, see here.