Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 592
Fakebear Avatar asked Jun 10 '11 08:06

Fakebear


People also ask

What is the use of AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout. DatePickerDialog or TimePickerDialog. A dialog with a pre-defined UI that allows the user to select a date or time.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

When a dialog is displayed on top of your activity what will happen?

Displaying the dialog-as-an-Activity will cause the new Activity to be on the top of the stack, pausing what previously was there. There are two more such scenarios when onPause() will get called.


2 Answers

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

like image 43
Mike Cwiklinski Avatar answered Oct 04 '22 07:10

Mike Cwiklinski


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.

like image 120
Moses Avatar answered Oct 04 '22 07:10

Moses