3

Basically, I'm trying to recreate the functionality of the MediaController in the native Music app (that I see on 2.2), where the back button immediately backs out of screen, instead of hiding the MediaController. There doesn't seem to be any good way to set a keylistener or override a method to intercept these keyevents though.

Any ideas?

David Liu
  • 9,426
  • 5
  • 40
  • 63

1 Answers1

1

You can try something like

mMediaController = new MediaController(this) {

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            // TODO Auto-generated method stub

            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                finish();

            }
            return super.dispatchKeyEvent(event);
        }
} 

or the dispatchKeyEvent like

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    ContentActivity.this.dispatchKeyEvent(event);
}
dorjeduck
  • 7,624
  • 11
  • 52
  • 66
  • The dispatchKeyEvent is being invoked twice for single backpress any idea why? – Jasper Jan 06 '15 at 18:38
  • @Jasper, because it reacts first on ACTION_DOWN and then on ACTION_UP. If you want to handle it only once, use `event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP` – Nikita Khlebushkin Mar 03 '16 at 10:14