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();

    }

}
1

5 Answers 5

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);
        }
    });
1
  • 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!) Jul 2, 2015 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);
    }
}
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.

-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.

3
  • 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, 2011 at 17:04
  • That's not a valid statement at all.
    – afollestad
    Jun 22, 2015 at 21:20
  • finish(): "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, 2015 at 7:35
-2
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        System.exit(0);
    }

    return false;
}
1
  • System.exit() is an anti-pattern on Android.
    – afollestad
    Jun 22, 2015 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.