2

I have created a media player in android where the MediaPlayer is on a service. my main activity has an option menu with single item "exit" onOptionsItemSelected calls another method (mp is and instance of MediaPlayer in service)

private void exitPlayer() {
        PlayerService.mp.stop();
        onDestroy();
    }

and onDestroy method is simple

protected void onDestroy() {
        super.onDestroy();
        if (!PlayerService.mp.isPlaying()) {
            stopService(playerService);
            cancelNotification();
            finish();
        }   
    }

but it throws

java.lang.RuntimeException: Unable to destroy activity java.lang.IllegalStateException

can any one help me? thanks

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123
  • 2
    you are never suppose to call `onDestroy()` directly, its very bad – tyczj Nov 11 '13 at 18:49
  • An `IllegalStateException` usually contains more information that tells you exactly what happened. What does the rest of your logcat say? – Bryan Herbst Nov 11 '13 at 18:50
  • This might help you out: http://stackoverflow.com/questions/2176375/android-service-wont-stop – sulai Nov 11 '13 at 18:53
  • @tyczj mp.stop() is stoping the mediaplayer ! an why not calling onDestroy directly ? if I pause the music from activity and by back button exit the activity onDestory() is called without any exception I think this does the same hardcoded – Amir-Mousavi Nov 11 '13 at 19:01

3 Answers3

1

Instead of calling onDestroy() try this:

private void exitPlayer() {
     PlayerService.mp.stop();
     exitAll();
}

private void exitAll() {
    if (!PlayerService.mp.isPlaying()) {
        stopService(playerService);
        cancelNotification();
        finish();
}

The finish() will destroy the Activity. But you can't be sure that onDestroy() will be called! The system can destroy an Activity at any time like on low memory situations and onDestroy() will not be called.

The last callback which surely will be called is onPause(). So, move your code out of onDestroy() to be safe.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • I need 3 first lines of exitAll() in onDestroy() also , cause I dont want the back button pressed by user stops music any way thanks I was not thinking of like low memory situation nice hint – Amir-Mousavi Nov 11 '13 at 19:58
0

oh no such a silly mistake , finish() it self calls onDestroy() again so I had to simple change my code to :

private void exitPlayer() {
        if(PlayerService.mp.isPlaying())
        PlayerService.mp.stop();
        finish();
    }
protected void onDestroy() {
        super.onDestroy();
        if (!PlayerService.mp.isPlaying()) {
            stopService(playerService);
            cancelNotification();
        }

    }
Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123
0

This is not the perfect way to do but even if it's not a good practice the IllegalStateException could be avoid this way. (Use solution above from Steve)

Because onDestroy() is called at least. (as shown on this image : Activity lifecycle.)

your activity is almost finished at that time or being finished. by finish() .

So to use onDestroy() method without IllegalStateException, you will have to do so :

protected void onDestroy() {
    if (!PlayerService.mp.isPlaying()) {
        stopService(playerService);
        cancelNotification();
        //finish();
    } 
    super.onDestroy();  
}
AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
Co.
  • 1
  • 3