java - Cannot dismiss 2 AlertDialogs at once in Android -
i've built android alertdialog (named dialog) fires other android alertdialog (named dialog2) if conditions happen.
i've checked if 1 of them displayed on screen dismissed without problem.
problem comes when both of them showing, happens when press ok button second dialog, closes second dialog, despite first dialog showing on screen.
this code related dialog2 operation:
dialog2.setonshowlistener(new dialoginterface.onshowlistener() { @override public void onshow(final dialoginterface dialog) { button button = ((alertdialog) dialog).getbutton(alertdialog.button_positive); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { [some more operations] dialog2.dismiss(); dialog.dismiss(); } }); }
});
the strangest thing if supress line dialog2.dismiss(); leaving dialog.dismiss(); dismissed second dialog, not first one, looks android somehow confuses 1 other, , don't think should happening because created separately this:
dialog=[code create dialog] dialog2=[code create dialog]
doing way see app close dialog2 when asked close dialog doing dialog=dialog2, not. think should different objects loaded in memory each 1 characteristics.
i don't see reason of why happening, seems clueless error point of view. hope can give ideas happening.
a couple things take note of here:
first, isn't necessary create "onshowlistener", unless need perform task when dialog shown, code should correctly create alertdialog:
new alertdialog.builder(getcontext()) .settitle(r.id.dialog_title) .setmessage(r.id.dialog_message) .setpositivebutton(r.id.positive_text, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { //do onclick stuff here } }) .show();
this example setup @ once. if need reference dialog or don't want show right away, use alertdialog.builder dialog1 = new ...
, , use dialog1.show()
create dialog.
second, reason second dialog closing when suppress dialog2.dismiss() because there local variable named 'dialog' inside of onshow() method (look @ method parameters) taking precedence on broader scoped 'dialog' variable.
third, answer actual question, can dismiss first dialog before show second? don't see real reason have 2 dialogs open @ same time.
Comments
Post a Comment