简体   繁体   中英

Detect Back Button Event when Dialog is Open

I'm building an app that has 2 dialogues that open up and I want something to occur if the user presses the back button while certain dialogues are open. However, for some reason, the back button event is not registering when the dialogues are open. I tested it by putting a log in onBackPressed() and whenever the dialogues are NOT open and I'm simply on the main activity, the logs appear on logcat. However, if the dialogues are open, I simply get this:

W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.

Below I have placed the code for the dialogues:

    public void pair() {
        final Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

        AlertDialog.Builder pairedList = new AlertDialog.Builder(this);
        pairedList.setTitle("Paired Devices");

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice);
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                arrayAdapter.add(device.getName());
            }
        }

        pairedList.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                mBluetoothAdapter.disable();
//                pair_dialog = false;
            }
        });

        pairedList.setPositiveButton("Pair New", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivityForResult(new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS), 0);
            }
        });

        pairedList.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                connect_dialog = true;
                String strName = arrayAdapter.getItem(which);
                AlertDialog.Builder builderInner = new AlertDialog.Builder(MainActivity.this);
                builderInner.setMessage(strName);
                builderInner.setTitle("Connect To:");
                builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (BluetoothDevice device : pairedDevices) {
                            if(device.getName().equals(strName)){
                                paired = device;
                                dialog.dismiss();
                            }
                        }
                    }
                });
                builderInner.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        connect_dialog = false;
                        pairedList.show();
                    }
                });
                builderInner.show();
            }
        });
        pairedList.show();
//        pair_dialog = true;
    }

Below is my onBackPressed() method which is right after the above method. Nothing out of the ordinary, I don't think.

   @Override
    public void onBackPressed() {
        Log.e(TAG, "Back Button Pressed");
        super.onBackPressed();
    }

Like I said, if the dialogues are not open, the log shows up just fine in logcat but if the dialogues are open, it's like the back button doesn't register.

this worked for me...

       yuordialog.setOnKeyListener(new Dialog.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface arg0, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                   //your stuff....
                }
                return true;
            }
        });

If you have added,

dialog.setCancelable(false);

change it to,

dialog.setCancelable(true);

Actually, setCancelable(false) cancel the event of touch outside the dialog and back press also.

You can also use

builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        //your dismiss code here
    }
});

This listens to both backpress events and dismiss by touch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM