The AlertDialog is invisible when the Activity bac

2019-04-08 00:17发布

问题:

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.

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

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.

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

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. 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.

回答1:

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. That worked for my application.



回答2:

I think, there is some king of bug in Dialog. I have the same situation, and only solution was dismiss curent dialog by dialogDismiss(id) and show it again by showDialog(id).