0

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?

7
  • an AlertDialog to inform the user of what step of the startup process it's currently on That 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. Commented Nov 14, 2023 at 19:40
  • ProgressDialog is 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 from sleep but 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. Commented Nov 15, 2023 at 9:43
  • @blackapps Have you got an idea how to solve this problem? Commented Nov 17, 2023 at 11:10
  • Post complete code that we can copy/paste to try out. Commented Nov 17, 2023 at 16:40
  • @blackapps Can't post the whole class here. You can find the important code in my question, it's the loop. Remove the "log" part and add dialog.setMessage directly, so: for(String f:filenames) { dialog.setMessage("Something"+System.lineSeparator()+f); } Commented Nov 20, 2023 at 14:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.