7

I am having difficulty getting the back button to actually finish my activity when pressed. I am running a very simple videoview, using a progressdialog to show loading dialog and onpreparedlistener, etc etc. simple stuff. Anyways, currently when I press the back button, it will just cancel the progressdialog, and leave a black screen, and pressed again, the progressdialog restarts!!! and then when I click the back button again, it displays an alert dialog, "video cannot be played." very annoying. Thanks for your help.

public class VideoActivity extends Activity {

    private VideoView mVideoView;

    private static ProgressDialog progressdialog;
    private String path;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videoview);


        progressdialog = ProgressDialog.show(this, "", " Video Loading...", true);
        progressdialog.setCancelable(true);

        mVideoView = (VideoView) findViewById(R.id.surface_view);
        mVideoView.setMediaController(new MediaController(this));
        Bundle b = this.getIntent().getExtras();
        path = b.getString("path");
        mVideoView.setVideoURI(Uri.parse(path));


        mVideoView.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                progressdialog.dismiss();
                mVideoView.requestFocus();
                mVideoView.start();

            }
        });

    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();

        super.finish();

    }

}
benbeel
  • 1,532
  • 4
  • 24
  • 38
  • possible duplicate of [Android back button and MediaController](http://stackoverflow.com/questions/6051825/android-back-button-and-mediacontroller) – prolink007 Aug 02 '12 at 14:46

5 Answers5

15

You can simply write: (No need to create new class for MediaController)

mVideoView.setMediaController(new MediaController(this){
        public boolean dispatchKeyEvent(KeyEvent event)
        {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
                ((Activity) getContext()).finish();

            return super.dispatchKeyEvent(event);
        }
    });
rraval
  • 1,007
  • 1
  • 9
  • 11
Serge Him
  • 936
  • 6
  • 8
  • 5
    It appears that the VideoView consumes the ACTION_DOWN event, and thus ACTION_UP never fires into the dispatchKeyEvent(). This can be easily remedied by returning `false` on the ACTION_DOWN for KEYCODE_BACK instead of calling into the super. (But make sure to allow all other KeyCodes to call into the super!) – Paul Lammertsma Jul 02 '15 at 10:40
3

You'll want to create a custom MediaController class and override the dispatchKeyEvent function to capture the back KeyEvent and tell the activity to finish.

See Android back button and MediaController for more info.

public class CustomMediaController extends MediaController {
    public CustomMediaController(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomMediaController(Context context, boolean useFastForward) {
        super(context, useFastForward);
    }

    public CustomMediaController(Context context) {
        super(context, true);
    }

    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
            ((Activity) getContext()).finish();

        return super.dispatchKeyEvent(event);
    }
}
Community
  • 1
  • 1
Jeremy White
  • 2,818
  • 6
  • 38
  • 74
0

From CommansWare

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.

Here is the link to the original post.

Community
  • 1
  • 1
prolink007
  • 33,872
  • 24
  • 117
  • 185
-2
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        System.exit(0);
    }

    return false;
}
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Anu
  • 553
  • 5
  • 14
-2

finish() doesn't kill your activity, it just signals to Android that it doesn't need to run the Activity anymore.

I remember solving this by putting "return" in proper places.

Kheldar
  • 5,361
  • 3
  • 34
  • 63
  • I feel like the issue is that back press is just cancelling the videoview, and the activity is restarting videoview again. I can't seem to override this either...tricky – benbeel Aug 26 '11 at 17:04
  • That's not a valid statement at all. – afollestad Jun 22 '15 at 21:20
  • [`finish()`](https://developer.android.com/reference/android/app/Activity.html#finish%28%29): "Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult()." – Codecat Jun 25 '15 at 07:35