Dialog

cancel and dismiss in Dialog

Show the Code:

public void cancel() {
        if (!mCanceled && mCancelMessage != null) {
            mCanceled = true;
            // Obtain a new message so this dialog can be re-used
            Message.obtain(mCancelMessage).sendToTarget();
        }
        dismiss();
    }

So, if not set the cancel listener, cancel() = dismiss().

OnCancelListener and OnDismissListener

We need show some code for this:

public void dismiss() {
        if (Looper.myLooper() == mHandler.getLooper()) {
            dismissDialog();
        } else {
            mHandler.post(mDismissAction);
        }
    }

    void dismissDialog() {
        if (mDecor == null || !mShowing) {
            return;
        }

        if (mWindow.isDestroyed()) {
            Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!");
            return;
        }

        try {
            mWindowManager.removeViewImmediate(mDecor);
        } finally {
            if (mActionMode != null) {
                mActionMode.finish();
            }
            mDecor = null;
            mWindow.closeAllPanels();
            onStop();
            mShowing = false;

            sendDismissMessage();
        }
    }

So, The CancelListener will be called before cancel/dismiss, and DismissListener wil be called after dismiss.

Window Type for display dialog on keyguard

If you want to display a dialog on all screen, include the keyguard screen, now, you should set the window type for the dialog.

final Window win = mNoteDialog.getWindow();
            win.setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);

AlertDialog positive, negative and neutral button

AlertDialog will be disappear when click on the any button, such as positive, negative or neutral button. Because:

private final View.OnClickListener mButtonHandler = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Message m;
            if (v == mButtonPositive && mButtonPositiveMessage != null) {
                m = Message.obtain(mButtonPositiveMessage);
            } else if (v == mButtonNegative && mButtonNegativeMessage != null) {
                m = Message.obtain(mButtonNegativeMessage);
            } else if (v == mButtonNeutral && mButtonNeutralMessage != null) {
                m = Message.obtain(mButtonNeutralMessage);
            } else {
                m = null;
            }

            if (m != null) {
                m.sendToTarget();
            }

            // Post a message so we dismiss after the above handlers are executed
            mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialogInterface)
                    .sendToTarget();
        }
    };

So, setPositiveButton() == setNeutralButton() == setNegativeButton(), except display the different position from left, central and right when two or three buttons in dialog.

results matching ""

    No results matching ""