Implicit "cancel" command for AlertDialog (tap outside, back button)

4 views
Skip to first unread message

P5music

unread,
Aug 27, 2020, 2:28:25 PM8/27/20
to CodenameOne Discussions
My app features a popup menu (contextual menu) on certain elements.

I create this dialog (just some options are in the code snippet below):

public static String openContextualMenu()
{

Dialog alertDialog = new Dialog();
alertDialog.setLayout(BoxLayout.y());

Button editCommandButton=new Button(new Command(stringEdit));
Button deleteCommandButton=new Button(new Command(stringDelete));
Button exportCommandButton=new Button(new Command(stringExport));

alertDialog.add(editCommandButton).add(deleteCommandButton).add(exportCommandButton);

Command commandResult=alertDialog.showDialog();
String result=commandResult.getCommandName();
return result;
}

I think this is the right way to create a popup menu in Codename, isn't it?

However I would like to handle the case when the user dismiss the dialog with the back button or tapping outside. Is it possible? This could be useful also for other dialogs of my app.


Shai Almog

unread,
Aug 28, 2020, 1:03:45 AM8/28/20
to CodenameOne Discussions
Back command is handled with setBackCommand(). You can use setDefaultDisposeWhenPointerOutOfBounds() or setDisposeWhenPointerOutOfBounds() for the clicking outside.

Command backCommand = new Command("Back");
alertDialog.setBackCommand(backCommand);

Notice the command doesn't need to invoke dispose or anything. Commands implicitly dispose the dialog by default.
Reply all
Reply to author
Forward
0 new messages