Question

I'm having some problems after making an override of the method onDestroy. My app is a music player, using the instance of mediaplayer I need at some point to force the release of it if no music is playing. This is my code so far, for making the trick I've both overrided the onKeyDown() and onDestroy() method:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
       if(mp.isPlaying())
       {
           //Genera la notifica
           generateNotificationSong();
           //Muovi in background
           moveTaskToBack(true);
       }
       else finish();

       return true;
    }

    return super.onKeyDown(keyCode, event);
}


//Faccio un override della funzione onDestroy per evitare che il mediaplayer continui 
//a mandare musica in background, inoltre l'UpdateTimeTask risulta inutile
@Override
public void onDestroy()
{
        mNotify.cancel(001);
        if(mHandler != null)
            mHandler.removeCallbacks(mUpdateTimeTask); //rimuovo il thread che aggiorna la seekbar
        if(mp != null)
            mp.release(); //rilascio il media player

        super.onDestroy();

}

That's it, now when I want to close the application I simply press the back button and the apps calls the methods onPause() onStop() and onDestroy() right? Anyway sometimes happens that after the closing the phone freeze for 4-5 seconds and a message is displayed: "The program Application has been closed". I know I'm doing something wrong here but I don't know what, I need some help. Thanks in advice!

Was it helpful?

Solution

super.onDestroy() must be the first call of the onDestroy method if you override it.

OTHER TIPS

Try the below code it works for me

@Override

public void onDestroy() {

    mediaPlayer.stop();

    super.onDestroy();

        }

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top