Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 62
Registered: ‎07-15-2008
My Device: Not Specified
Accepted Solution

How to dismiss a dialog?

Hello,

 

I display a Dialog with a Yes and No option

 

UiApplication.getUiApplication().invokeAndWait( new Runnable() { public void run() { int response = Dialog.ask( Dialog.D_YES_NO, "Do you want to continue?" ); // waits here for user interaction if ( response == Dialog.YES ) { // do the task

} else { // cancel the task

} } } );

 

 The requirement of the application is to go ahead with the task after 5 seconds if the user does not choose an option. I do this by creating a timer task as the dialog is displayed and when the timer expires I continue with the task.

 The problem with this is that the Dialog, even though no longer displayed, is still awaiting user interaction. Is there a way of getting the Dialog to complete or is there something else I can use that will timeout? 

 

New Developer
Posts: 20
Registered: ‎07-18-2008
My Device: Not Specified

Re: How to dismiss a dialog?

Here are some sample pseudo codes which may help.

 

Dialog dlg = new Dialog(...); class DCListener implements DialogClosedListener { public int choice; public void dialogClosed(Dialog dialog, int choice) { this.choice = choice; } } DCListener dcl = new DCListener(); dlg.setDialogClosedListener(dcl); dlg.select(Dialog.XXX); pushScreen(dlg); class Task implements Runnable { Dialog dlg; DCListener dcl; public Task(Dialog dlg, DCListener dcl) { this.dlg = dlg; this.dcl = dcl; } public void run() { for (int i = 0; i < 5; i++) { // Check to see whether the catalog is closed ... } // If not closed, call UiApplication.popScreen(dlg) ... } } new Thread(new Task(dlg, dcl)).start();

 

 

 

Developer
Posts: 19,623
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to dismiss a dialog?

And some more that might do the job.  I quite like this, because it is self contained.

 

I'm not sure that the code is self explanatory, please ask if anything is not clear.

 

Note that I have NOT tested this, but I do something similar so I suspect this should work.  If you have any problems I will test and fix (if I can).

 

UiApplication.getUiApplication().invokeAndWait( new Runnable() { public void run() { final Dialog d = new Dialog("Do you want to continue?", new String [] { "Yes", "No" }, new int [] { Dialog.YES, Dialog.NO }, Dialog.YES, Bitmap.getPredefinedBitmap(Bitmap.QUESTION)); Thread timerThread = new Thread() { public void run() { while ( !d.isDisplayed() ) { try { sleep(1000); } catch (Exception e) { } } // Above might be needed to make sure we won't start 5 sec wait // until the Dialog is actually displayed try { sleep(5000); } catch (Exception e) { } if ( d.isDisplayed() ) { // Dialog has not been automatically dismissed // Automatically select YES d.select(Dialog.YES); } } }; timerThread.start(); d.doModal(); // Dialog will be forced close after 5 seconds by timerThread int response = d.getSelectedValue(); if ( response == Dialog.YES ) { // do the task } else { // cancel the task } } } );

 

Developer
Posts: 19,623
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to dismiss a dialog?

Spookily, I've just had to code something like this, so I found my deliberate mistake. 

To stop the IllegalStateException, replace the line d.select(Dialog.YES); with UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { d.select(Dialog.YES); } });

 

Developer
Posts: 62
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to dismiss a dialog?

Excellent work Patrick and Peter, that worked a treat. Thank you