Search code examples
androidservicemedia-playerandroid-listviewondestroy

Android- Destroy media player service when click on item in Listview


I currently have a listview and when you click on an item it runs a service with a mediaplayer. If I click on another item in the listview the service that's running should stop and run the new service. What would be the best way of handling this? Would it be to use the onDestroy? Thanks a lot.


Solution

  • If you ask me you should not stop and restart service like this but i don't know the exact situation as may be its the necessity of your app.

    You should call context.stopService(yourServiceIntent) in your ListView's onItemClick() then restart the service with new input using putExtra() in the same onItemClick().

    But i still recommend you to find an alternative to this service stop and restart procedure as it could affect the performance of your app by decreasing the response time.