1

I have created a window and i am showing it on screen through Broadcast receiver.But the problem is that it appears on the screen and i want to dismiss it once the back button is pressed.I am unable to get the event of button press on this view.My code for back press looks as follows-

  view.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getKeyCode() == KEYCODE_BACK) {
                Log.d("LOG", "back button is pressed");

            }
            return true;
        }
    });  

But Nothing is happening.I tried to do the same through DISPATCHKEY but it was also of no use.Please help me what i am not figuring out.Wouldn't this work on the view.?

Vipin NU
  • 301
  • 2
  • 16
  • Maybe [This](https://stackoverflow.com/questions/30861075/back-pressed-events-with-system-alert-window) will help. – ADM Jan 12 '18 at 07:02

1 Answers1

1

Maintain global reference for Window and override onBackPressed() Try this:

@Override
 public void onBackPressed() {
  if (view != null && view.isShowing()) {
    view.dismiss();
  } else {
    super.onBackPressed();
  }
}