33

I'm managing dialogs by showDialog/dismissDialog/removeDialog.

I want to:

Display several dialogs in kind of a stack:

a) First dialog is shown using showDialog(DIALOG_TYPE)

b) Next dialogs are shown on top of the existing dialog

Now I'm only able to display first dialog using showDialog and then next dialogs are ignored.

Display last dialog:

a) First dialog is shown using showDialog(DIALOG_TYPE)

b) Application checks if dialog is displayed, closes dialog (if it's displayed) and opens a new dialog.

Is there any possibility to achieve one of the above solutions?

pixel
  • 24,905
  • 36
  • 149
  • 251

3 Answers3

72

Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog(). You just have to keep a reference to the Dialogs you create in onCreateDialog().

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • Ok, this satisfies the first scenario, but what about stacking? Maybe there is some possibility to show several dialogs of the same type? – pixel Jun 30 '10 at 20:28
  • 5
    Doesn't seem to work for stacking. If I stack multiple dialogs on top of each other, both have true for the showing property. – Lance Nanek Oct 15 '13 at 18:09
  • I don't know why calling *getDialog().isShowing()* from DialogFragment make it can't be showed, it's so crazy. – Phong Nguyen Apr 19 '19 at 03:42
  • @PhongNguyen DialogFragment has `isVisible()` – 6rchid Jun 18 '23 at 03:50
4

Dialog implements DialogInterface which has OnShowListener.

Therefore you can use code like this:

Dialog dialog = new Dialog(context);
// ... set all things considering your dialog
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        // do something when your dialog is shown    
    }
});
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
4

You can use flag to check whether dialog is opened or not and according to flag value you can do whatever you want. Like I did. I am having only one dialog but when I touch another EditText and if my dialog is opened then it should close first and then should re-open with animation.

Code snippet:

    private EditText mEditText, mEditCode;
    private Dialog mDialog;
    private int mClicked = 0;
    private boolean isShown = false;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mEditText = (EditText)findViewById(R.id.EnterValue);
        mEditText.setClickable(true);
        mEditText.setOnClickListener(this);

        mEditCode = (EditText)findViewById(R.id.EnterCode);
        mEditCode.setClickable(true);
        mEditCode.setOnClickListener(this);
    }

    public void onClick(View nView)
    {
        switch (nView.getId()) 
        {
        case R.id.EnterValue:
            mClicked = R.id.EnterValue;
            mEditText.requestFocus();
            mEditText.setFocusableInTouchMode(false);
            mEditText.setEnabled(true);
            mEditText.setSelection(mEditText.getText().toString().trim().length());
            if(isShown)
            {
                mDialog.dismiss();
                showInfoDialog();
            }
            else
            {
                showInfoDialog();
            }
            break;
        case R.id.EnterCode:
            mClicked = R.id.EnterCode;
            mEditCode.requestFocus();
            mEditCode.setFocusableInTouchMode(false);
            mEditCode.setEnabled(true);
            mEditCode.setSelection(mEditCode.getText().toString().trim().length());
            if(isShown)
            {
                mDialog.dismiss();
                showInfoDialog();
            }
            else
            {
                showInfoDialog();
            }
            break;
        }
    }

    private boolean showInfoDialog() 
    {
        mDialog = new Dialog(CustomKeyboardNotLikeAndroidActivity.this, R.style.PauseDialog);
        mDialog.setContentView(R.layout.keyboard);
        mDialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
        mDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        mDialog.setCancelable(true);
        isShown = true;
        mDialog.show();
        return false;
    }

Try to alter this code in your way. Hope this can help you. Thanks.