oikonomopo
oikonomopo

Reputation: 4065

alert dialog after another alert dialog ? The first is missing! android

I have an activity(ImportActivity) ,where user inputs some values for a measure and save them into a sqlite database.

When the user clicks save button after import to the database i have an alert dialog(SAVEORBACK_DIALOG_ID),where the user can leave this activity or import another measure.It works perfect.

My problem is when i try to show another alert dialog(SMS_DIALOG_ID) just before (SAVEORBACK_DIALOG_ID) alert dialog.This because i want to ask the user to send or not an sms.

When i run this it shows me only the second alert dialog(SAVEORBACK_DIALOG_ID)!!

ImportActivity this is final dialog

I have in the activity:

static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;
static final int SAVEORBACK_DIALOG_ID = 2;
static final int SMS_DIALOG_ID = 3;

I call them from my activity :

// sms dialog (send sms to doctor? yes/no)
showDialog(SMS_DIALOG_ID);

// save or back dialog
showDialog(SAVEORBACK_DIALOG_ID);

Here is the onCreateDialog method where I have my dialogs(I removed some to be easier to read):

    @Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                mDay);
    case TIME_DIALOG_ID:
        return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
                false);
    case SAVEORBACK_DIALOG_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(
                "Information saved successfully ! Add Another Info?")
                .setCancelable(false)
                .setPositiveButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                ImportActivity.this.finish();

                            }
                        })
                .setNegativeButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                                // get the new date
                                // Clearing the fields & update date/time
                                // textviews
                            }
                        });
        AlertDialog dialog = builder.create();
        return dialog;

        // case sms dialog
    case SMS_DIALOG_ID:
        AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
        builder2.setMessage("High blood pressure ! Send sms to doctor?")
                .setCancelable(false)
                .setPositiveButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                                // do nothing - just continue
                            }
                        })
                .setNegativeButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                                // try to send sms - report status
                            }
                        });
        AlertDialog dialog2 = builder2.create();
        return dialog2;
        //

    }
    return null;
}

Upvotes: 4

Views: 6804

Answers (1)

Sam
Sam

Reputation: 86948

These commands are executed consecutively, which as you noticed, overwrites the first dialog:

    // sms dialog(send sms to doctor?yes/no)
    showDialog(SMS_DIALOG_ID);

    // save or back dialog
    showDialog(SAVEORBACK_DIALOG_ID);

because showDialog() does not wait for a response from the first dialog before displaying the second.

Simply move your second showDialog() command to the Buttons in your first dialog:

.setPositiveButton("No",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
            // do nothing - just continue
            ImportActivity.this.showDialog(SAVEORBACK_DIALOG_ID);
        }
    })
// etc, etc

Alternatively you can use an OnDismissDialogListener, this is called whenever the dialog is closed (via a Button click, cancel action, etc.):

case SMS_DIALOG_ID:
    ...
    AlertDialog dialog2 = builder2.create();

    dialog2.setOnDismissListener(new OnDismissListener() {
        public void onDismiss(DialogInterface dialog) {
            ImportActivity.this.showDialog(SAVEORBACK_DIALOG_ID);
        }
    });

    return dialog2;
}

Upvotes: 8

Related Questions