Android: How to get a modal dialog or similar modal behavior? Android: How to get a modal dialog or similar modal behavior? android android

Android: How to get a modal dialog or similar modal behavior?


I got a modal Dialog while using:

setCancelable(false);

on the DialogFragment (not on the DialogBuilder).


It is not possible the way you planned. First, you are not allowed to block the UI thread. Your application will be terminated. Second, need to handle the lifecycle methods that are called when another activity is started with startActivity (your original acitvity will be paused while the other activity is running). Third, you probably could somehow hack it by using startAlertDialog() not from the UI thread, with thread synchronization (like Object.wait()) and some AlertDialog. However, I strongly encourage you to not do this. It is ugly, will certainly break and it's just not the way things are intended to work.

Redesign your approach to capture the asynchronous nature of these events. If you want for example some dialog which asks the user for a decsision (like accepting the ToS or not) and do special actions based on that decision create a dialog like this:

AlertDialog dialog = new AlertDialog.Builder(context).setMessage(R.string.someText)                .setPositiveButton(android.R.string.ok, new OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        dialog.dismiss();                        // Do stuff if user accepts                    }                }).setNegativeButton(android.R.string.cancel, new OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        dialog.dismiss();                        // Do stuff when user neglects.                    }                }).setOnCancelListener(new OnCancelListener() {                    @Override                    public void onCancel(DialogInterface dialog) {                        dialog.dismiss();                        // Do stuff when cancelled                    }                }).create();dialog.show();

Then have two methods handling positive or negative feedback accordingly (i.e. proceeding with some operation or finishing the activity or whatever makes sense).


Developers of Android and iOS decided that they are powerful and smart enough to reject Modal Dialog conception (that was on market for many-many years already and didn't bother anyone before), unfortunately for us.

Here is my solution, it works great:

    int pressedButtonID;    private final Semaphore dialogSemaphore = new Semaphore(0, true);    final Runnable mMyDialog = new Runnable()    {        public void run()        {            AlertDialog errorDialog = new AlertDialog.Builder( [your activity object here] ).create();            errorDialog.setMessage("My dialog!");            errorDialog.setButton("My Button1", new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface dialog, int which) {                    pressedButtonID = MY_BUTTON_ID1;                    dialogSemaphore.release();                    }                });            errorDialog.setButton2("My Button2", new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface dialog, int which) {                    pressedButtonID = MY_BUTTON_ID2;                    dialogSemaphore.release();                    }                });            errorDialog.setCancelable(false);            errorDialog.show();        }    };    public int ShowMyModalDialog()  //should be called from non-UI thread    {        pressedButtonID = MY_BUTTON_INVALID_ID;        runOnUiThread(mMyDialog);        try        {            dialogSemaphore.acquire();        }        catch (InterruptedException e)        {        }        return pressedButtonID;    }