Search code examples
androidandroid-homebutton

Android: Stop playback on Home or Back button


When I press back or home in my application, it hides, but the MediaPlayer keeps going. Is there a way to know when these buttons have been pressed and stop playback before closing?


Solution

  • You should be able to override the onPause() function in your Activity. onPause() will be called when you Activity is hidden and you can pause the MediaPlayer there.

    For example,

    @Override
    protected void onPause() {
        super.onPause(); // Don't forget this line
        myMediaPlayer.pause() // Or whatever the function is to pause it
    }