7

I created an AlertDialog to show the user next level challenges when one is succeeded. So, the corresponding code is like this. when the game is succeeded showDialog(R.id.display_success) is called and the following code is executed.

So, I am expecting to execute this code in every call. However; the game is executing only once and showing the same AlertDialog in every other execution. I mean, like the instance is not created after the first one is created and the first instance is used all the time.

case R.id.display_success:           
       updateGameSettings();
       message = formatLevel()
       + formatMission();
       return new AlertDialog.Builder(this)
       .setIcon(R.drawable.smiley_happy)
       .setTitle(R.string.dialog_success)
       .setMessage(message)
       .setPositiveButton(R.string.alert_dialog_newgame, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog,     int whichButton) {
                       startANewGame();
               }
       })
       .setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int whichButton) {
                     finish();
               }
       })
       .create();
Athira
  • 1,177
  • 3
  • 13
  • 35
Ömer
  • 1,666
  • 2
  • 18
  • 33

2 Answers2

4

I think I have a fix for the inconsistent behavior of onPrepareDialog. When initially creating the dialog (when it's still an AlertDialog.Builder), you have to set the message to an initial state (not null) or onPrepareDialog will NOT overwrite the message with the intended value. So when you're creating the dialog, do something like this to always have a non-null value in the message. I struggled with this for days and found this solution by accident:

AlertDialog.Builder resultAlert = new AlertDialog.Builder(context);

if ( message == null ) {
    resultAlert.setMessage("");
} else {
    resultAlert.setMessage(message);
}
Jerry Destremps
  • 448
  • 4
  • 6
  • Awesome, that works! Though for some reason, it doesn't work for the title of the dialog... – matt Dec 21 '11 at 20:09
3

onPrepareDialog method is called when the dialog is shown. So, it is better to change the text or other features by overriding this method.

Ömer
  • 1,666
  • 2
  • 18
  • 33
  • 1
    I think you are wrong. By design, every Dialog only is created once. If you want to change the contents of your dialog use onPrepareDialog, which gets called everytime your dialog is about to show up. The provided code might work, but it is not the preferred way to do it. – Tim Büthe Nov 12 '10 at 10:12
  • I put the 'removeDialog' in a try catch statement. The code works, but I very much doubt it's the right way to do it – Todd Davies Jun 30 '11 at 19:53
  • @Tim, yes I am wrong. I learnt this much later that onPrepareDialog is called every time the dialog is created. – Ömer Jul 05 '11 at 08:56
  • 1
    Thank you @Tim. You are so kind. Actually, I will be happy to accept your answer, if you write it down. :) Thanks again. – Ömer Jul 06 '11 at 06:39
  • Answering your own question is totally fine we should just leave it that way. Cheers :-) – Tim Büthe Jul 07 '11 at 07:50