Override back button in android

可紊 提交于 2019-11-28 01:16:50

I don't know if this is your problem, but when you call onBackPressed(); in your onkeydown, you are not returning, so the parent.onkeydown is also called, and the 'normal' back is just being 'executed'.

Insert a return statement there so you will not do the normal function from the parent class.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
            && keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        Log.d("CDA", "onKeyDown Called");

        onBackPressed();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

for Handleing All key use

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
       //Things to Do
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!