Android onkeydown() not working

1.7k views Asked by At

I have an activity where I flag full-screen once the orientation changes, problem is when you are currently in full-screen and you press the back button it will go back to the previous fragment in full-screen, which I do not want. I added this code but it doesn't seem to be called:

public boolean onKeyDown(int keyCode, KeyEvent event) {
  // TODO Auto-generated method stub
  if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
    //do what ever you want here
    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    getActivity().getActionBar().show();
    getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    Toast.makeText(getActivity().getBaseContext(), "backbuttonpressed", Toast.LENGTH_LONG).show();
    getActivity().finish();
    return true;
  }
  return super.getActivity().onKeyDown(keyCode, event);
}
1

There are 1 answers

0
Robin Dijkhof On

Maybe this will work for you:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Setting s = Utils.readSettings(this);
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}