Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android back button and MediaController

I know how to take control of the back button. I have a VideoView embedded in a FrameLayout. My question is when the video pops up, the video controls are present for a few seconds. Hitting the back button while they are visible hides the video controls. Is there a way to ignore that function and do the next back action as if the video controls weren't visible?

The reason I ask is if I really do want to go back, I must hit the back button twice; once to hide the controls and second to actually go back

like image 334
Ronnie Avatar asked May 18 '11 22:05

Ronnie


People also ask

Do all Android devices have a back button?

All Android devices provide a Back button for this type of navigation, so you should not add a Back button to your app's UI. Depending on the user's Android device, this button might be a physical button or a software button.

What is the difference between the up button and back button?

When you press the Back button, the current destination is popped off the top of the back stack, and you then navigate to the previous destination. The Up button appears in the app bar at the top of the screen.


2 Answers

Based on the source code, this should work:

  1. Extend MediaController (for the purposes of this answer, call it RonnieMediaController)
  2. Override dispatchKeyEvent() in RonnieMediaController
  3. Before chaining to the superclass, check for KeyEvent.KEYCODE_BACK, and if that is encountered, tell your activity to finish()
  4. Use RonnieMediaController instead of MediaController with your VideoView

Personally, I'd just leave it alone, as with this change your user cannot make a RonnieMediaController disappear on demand.

like image 164
CommonsWare Avatar answered Oct 08 '22 21:10

CommonsWare


You can simply write:

mVideoView.setMediaController(new MediaController(this){         public boolean dispatchKeyEvent(KeyEvent event)         {             if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)                 ((Activity) getContext()).finish();              return super.dispatchKeyEvent(event);         }     }); 

No need to create new class.

like image 34
Serge Him Avatar answered Oct 08 '22 22:10

Serge Him