该AlertDialog是无形的活动时回到前台前台、AlertDialog

2023-09-05 08:27:16 作者:不羁

我写一个程序,支持下载文件。当这个下载活动,我开始一个进度条,然后运行一个线程来执行下载的东西。此线程发送消息给UI线程来通知有多少经常被下载。在主(UI)线程,当我收到消息更新进度条显示。如果有任何问题发生在下载进度,它将发送另一条消息。当主线程收到消息,它停止了进度条和弹出一个新的AlertDialog显示错误原因。

I write a program support download file. When in this download activity, I start a progress bar and then run a thread to do the download things. This thread send message to UI thread to notify how many has been downloaded frequently. In the main(UI) thread, I update the progress bar display when receive the message. If any problem happen in download progress, it will send another message. When main thread receive the message, it stop the progress bar and pop up a new AlertDialog to show the error reason.

下面是它一个特殊的测试。下载时,切换程序的设置。关闭WIFI / GPRS使网络了。

Here is a special test for it. When downloading, switch the program to settings. Turn off the WIFI/GPRS to make the network off.

当我回到我的程序,它应该显示背景的用户界面和弹出AlertDialog显示,因为我希望的原因。但它仅显示背景的ui(这意味着主要活动),并用半光的背光源,正如弹出窗口仍然存在。当我preSS回来第一次,什么也没有发生,除了背景明亮,就像我有密切的弹出窗口。

When I back to my program, it should display the background ui and a pop up AlertDialog to show the reason as I wish. But it only display the background ui(which means the main activity) and with a half-light of backlight just as the popup windows still there. When I press back for first time, nothing happens except the backlight is bright just like I have close a pop up window.

我想,也许这是因为当我开始AlertDialog我的活动是不是在前台。

I think maybe it is because when I start the AlertDialog my activity is not in foreground.

我试图用:

ActivityManager am = (ActivityManager)Update.this.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;

要判断在顶是否是我的。但我想显示对话框。如果活动不被用户看到,当他回到我的活动,我希望它显示对话框。

to judge whether the top activity is mine. But I want to show the dialog. If the activity isn't being seen by the user, when he gets back to my activity, I want it to show the dialog.

推荐答案

事实上对话已经示出,但不可见的用户,如果你旋转设备将变得可见。

Indeed the dialog already shown but not visible to the user and it would become visible if you rotate the device.

似乎有一个刷新/绘图问题,如果驳回进度对话框,并立即显示出另一种新的对话框时,该活动是不是在前台。 在我的测试中,这样的问题不会发生,如果等到完成的进度对话框的解雇行为,然后显示新的对话框。

There seemed a refresh/drawing issue if dismiss a progress dialog and show another new dialog immediately when the activity is not in foreground. During my testing, such issue not happen if wait for the dismiss action finished for the progress dialog and then show the new dialog.

所以,一种解决方案是:第一,显示AlertDialog,然后关闭该ProgressDialog。这工作对我的申请。

So one solution is that show the AlertDialog first and then dismiss the ProgressDialog. That worked for my application.