android activity返回键被dialog屏蔽

这个问题遇到好几次了,一直没整理下来,这次记录下,先描述下:在一个activity里弹出一个dialog,这个时候点击返回键的时候想直接把activity finish掉,但是此时返回键的监听被dialog给屏蔽了,只能先finish掉dialog

解决方案:

只能监听dialog的back事件了

 dialog.setOnKeyListener(new OnKeyListener()
                    {
                        public boolean onKey(DialogInterface dialog,
                                int keyCode, KeyEvent event)
                        {
                            if (keyCode == KeyEvent.KEYCODE_BACK)
                            {
                                dialog.dismiss();

                             //此处把dialog dismiss掉,然后把本身的activity finish掉
                             //   BarcodeActivity.this.finish();
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    });

完美啊。。。。。。。。。。。。。。。。。。。。

你可能感兴趣的:(android开发)