My app (Java 11, minSDK 29, targetSDK 30) uses an AlertDialog to inform the user of what step of the startup process it's currently on, which includes showing the name of the file that's currently being downloaded (these files are required to use the app).
I call the following function from my first fragment right before starting each major step (this is working):
private void updateProgressDialog(String m) {
loadingDialogMessage = loadingDialogMessage + "\n" + m;
loadingDialog.setMessage(loadingDialogMessage);
}
I also pass the dialog to a second class (not an activity) that takes care of the downloads:
public void setAlert(AlertDialog a) {
dialog = a;
dialogOGMsg = ((TextView) dialog.findViewById(android.R.id.message)).getText().toString();
}
... then update the dialog from within the second class through this:
for(String f:fileNames) {
writeFilenameToLog(f);
updateAlert(f);
SystemClock.sleep(500); //this is only used for testing!
}
... with this:
private void updateAlert(String m) { //Called from the main thread, no asynchronous code involved yet!
//Log.d(TAG,"updateAlert: '"+m+"'");
Log.d(TAG,"msg='"+(dialogOGMsg+System.lineSeparator()+m)+"'");
dialog.setMessage(dialogOGMsg+System.lineSeparator()+m);
}
I haven't coded the actual download part yet, that's why I added sleep to see the file names change in the dialog - this is currently taking about 20 seconds to finish. The problem is: The dialog isn't updating properly (tested in the emulator and on an actual device), it only displays the very last file name once the loop is done, even though Log is printing the correct messages. This also happens without sleep.
I'm aware an AlertDialog probably takes a little bit of time to update but is there a reason why it would get completely stuck like that for that long? How do I make it update "properly" to display every file name during its specific loop iteration?
an AlertDialog to inform the user of what step of the startup process it's currently onThat looks like a bad idea. You also do it on the main thread and it will not be updated before it ends. Why not use that progress dialog the whole time. Or use notifications.ProgressDialogis deprecated and shouldn't be used anymore and notifications would spam the user, as there are a couple of steps (+downloads). As I said, the files are needed to use the app, there's no way around that and you won't get past the login screen without them, so why not at least tell the user how far it is.it will not be updated before it ends- why? Nothing's blocking the main thread (apart fromsleepbut it's not working without that either).use that progress dialog the whole time- what do you mean? The dialog is shown the whole time and I'm updating its message.dialog.setMessagedirectly, so:for(String f:filenames) { dialog.setMessage("Something"+System.lineSeparator()+f); }