点击返回键报错 Cancelling event due to no window focus: KeyEvent
// 监听手机返回键
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if (addRecordList.size() != 0 || addImgList.size() != 0 || !tvAcdLogText.getText().toString().trim().isEmpty()) {
dialog();
} else {
// 这里是核心代码,点击返回键如果不符合条件,不要马上关闭当前页面,否则就会报标题的错误,加一个延迟器即可
Handler mHandler= new Handler();
Runnable r1 = new Runnable() {
@Override
public void run() {
onBackPressed();
}};
mHandler.postDelayed(r1, 300);
}
}
return super.onKeyDown(keyCode, event);
}
网友评论