AlertDialog is invisible when activity returns to the forefront

I am writing a program download file. When in this download, I start the progress bar and then start the thread to execute the download. This thread sends a message to the user interface thread to notify how many of them have been downloaded frequently. In the main thread (UI), I am updating the progress bar display when I receive a message. If any problem occurs during the download process, it will send another message. When the main thread receives the message, it stops the progress bar and a new AlertDialog pops up to show the cause of the error.

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

When I get back to my program, it should display the ui background and the AlertDialog popup to show the reason as desired. But it only displays the background ui (which means the main action) and with a half-light backlight, just like pop-ups. When I press back for the first time, nothing happens, except that the backlight is bright, as if I have a closed pop-up window.

I think maybe this is because when I run AlertDialog, my activity is not in the foreground.

I tried using:

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

to judge whether my top activity is mine. But I want to show a dialogue. If the activity is not viewed by the user when he returns to my activity, I want it to show a dialog.

+6
source share
2 answers

Indeed, the dialog is already shown, but not displayed to the user, and it will become visible if you rotate the device.

There seems to be a problem updating / drawing if you cancel the progress dialog and show a new new dialog right away when the activity is not in the foreground. During my testing, this problem does not occur if you wait for the close action to complete for the progress dialog and then display a new dialog box.

So, one solution is to first show AlertDialog and then release ProgressDialog. This worked for my application.

+3
source

I think Dialog has some kind of error king. I have the same situation, and only the solution rejected the curent dialog from dialogDismiss(id) and shows showDialog(id) again.

0
source

Source: https://habr.com/ru/post/890212/


All Articles