简体   繁体   English

Activity 回到前台时,AlertDialog 不可见

[英]The AlertDialog is invisible when the Activity back to foreground

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.该线程向 UI 线程发送消息以通知已频繁下载了多少。 In the main(UI) thread, I update the progress bar display when receive the message.在主(UI)线程中,我在收到消息时更新进度条显示。 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.当主线程收到消息时,它会停止进度条并弹出一个新的 AlertDialog 以显示错误原因。

Here is a special test for it.这是一个特殊的测试。 When downloading, switch the program to settings.下载时,将程序切换到设置。 Turn off the WIFI/GPRS to make the network off.关闭 WIFI/GPRS 使网络关闭。

When I back to my program, it should display the background ui and a pop up AlertDialog to show the reason as I wish.当我回到我的程序时,它应该显示背景 ui 和一个弹出的 AlertDialog 以显示我希望的原因。 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.但它只显示背景 ui(表示主要活动)和半光背光,就像弹出窗口 windows 仍然存在一样。 When I press back for first time, nothing happens except the backlight is bright just like I have close a pop up window.当我第一次按下时,除了背光很亮,就像我关闭了弹出 window 一样,什么都没有发生。

I think maybe it is because when I start the AlertDialog my activity is not in foreground.我想可能是因为当我启动 AlertDialog 时,我的活动不在前台。

I tried to use:我尝试使用:

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.判断top活动是否是我的。 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.在我的测试期间,如果等待进度对话框的解除操作完成然后显示新对话框,则不会发生此类问题。

So one solution is that show the AlertDialog first and then dismiss the ProgressDialog.所以一种解决方案是先显示 AlertDialog 然后关闭 ProgressDialog。 That worked for my application.这对我的应用程序有用。

I think, there is some king of bug in Dialog .我认为, Dialog中有一些错误之王。 I have the same situation, and only solution was dismiss curent dialog by dialogDismiss(id) and show it again by showDialog(id) .我有同样的情况,唯一的解决方案是通过dialogDismiss(id)关闭当前对话框并通过showDialog(id)再次显示它。

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

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