0

I am trying to get a user input from a dialog prompt to determine wether a variable is true or false. I need to to do this before any other code in the List Activity class is executed.

Problem is no matter where I call the method that displays prompt, the rest of the code in the class continues to execute before the dialog returns a user choice!

Where is best place to place a dialog prompt, and stop code form executing until we get a response from the user via the dialog?

Thanks Ciaran

PS this is the code I call in the ListActovty onCreate method:

private void previewDives() {
        //ask user if they wish to preview images in ist view as will take more time
                AlertDialog.Builder build = null;
                AlertDialog dialog = null;
                build = new AlertDialog.Builder(this);
                build.setCancelable(true);
                build.setMessage("Do you want to preview your Dive Site images? \nThis may take more time if you have a large number of dives");
                build.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // preview dives
                                preiwImagesInListView= true;
                                value++;
                                //return;//exit

                            }// end on click
                        });// end pos button take photo

                // get photo from SD card option
                build.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                preiwImagesInListView=false;
                                value++;
                                //return;//exit to oncreate

                            }// end onclick
                        });// end pos button take photo
                dialog = build.create();
                dialog.setTitle("Preview Dive Images");
                dialog.show();

                //return preiwImagesInListView;


    }
dancingbush
  • 2,131
  • 5
  • 30
  • 66

2 Answers2

1

The dialog is executed in a different thread. So whatever you do after the show() call will be executed anyways. To solve this issue make a function to handle the response of the function and call that function inside the Yes and No answer function with a argument. A better solution is to use a MessageHandler to make it thread safe if you are using multiple threads.

You can not let the thread wait. More info can be found on this thread Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style)

Community
  • 1
  • 1
Niels
  • 48,601
  • 4
  • 62
  • 81
1

You can put your code into Dialog Yes button onClick() event. It's because after getting a data to confirm the user you want to processed more or not. but before executing your code into yes button onclick() you must check user input is valid or not.

M D
  • 47,665
  • 9
  • 93
  • 114