1

I am making a music player and I have a list with all the music. When you click on one it opens a new activity and plays the song, but if I go back to the list and choose another song it doesnt stop the first. How do I make it stop the music or close the first activity when I play a new song?

I have tried setting the launchMode to "singleTask" but that didnt work ..

1 Answers1

3

You can call finish() your Music playing activity in the onStop()-method. This will close the Activity and open a new one after you choose a new song to play.

A better way of doing this (since you're creating a multimedia player) would be to play the music in a Service (not in an Activity) and simply use the "player"-activity or the "playlist"-activity to change the played sound.

This way, the user can leave your app and still listen to his music. Also, it enables him to browser his music for a new track while listen to something else.


To communicate with your Service, you'll need to "bind" it. In your case, I'd suggest a Binder to do so. Read the "Bound Service"-article from the docs for more information/examples.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111