Tutorial :Waiting for DialogActivity to return before continuing executing of the main thread



Question:

How would I force the current thread to wait until another has finished before continuing.

In my program the user selects a MODE from an AlertDialog, I want to halt executing of the program before continuing as the mode holds important configuration for the gameplay.

        new AlertDialog.Builder(this)      .setItems(R.array.game_modes, new DialogInterface.OnClickListener() {          public void onClick(DialogInterface dialog, int which) {              switch (which) {              case 0:                  setMode(TRAINING_MODE);              case 1:                  setMode(QUIZ_MODE);              default:                  setMode(TRAINING_MODE);                  break;              }                //continue loading the rest of onCreate();              contineOnCreate();          }      })      .create().show();  

If this is impossible can anyone give a possible solution?


Solution:1

How would I force the current thread to wait until another has finished before continuing.

You don't.

In my program the user selects a MODE from an AlertDialog, I want to halt executing of the program before continuing as the mode holds important configuration for the gameplay.

No, you don't. You want to exit out of the main application thread (so the dialog appears), and then kick off gameplay after the user dismisses the dialog by one means or another.

because the Alert loads in it's own thread therefore allowing the main thread to continue loading as normal

No, it doesn't. All UI is done on the main application thread, dialogs included.

No because some of the onStart() code controls activity state when the Activity loses focus and needs to be re-configured when the activity re-gains focus.

Then don't do any of that until after the user has dismissed the dialog by one means or another.

Another option: move your "dialog" UI options into its own activity, and have users pass through it en route to the game activity.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »