I\'m working on a music app, and I want stop the service when user removes the application from the recent list and not when the first activity is destroyed(because when use
The last answer didn't work for me (I am using API 29). I looked up in the Javadoc of the onTaskRemoved function and saw the following instruction: "If you have set {android:stopWithTask}, then you will not receive this callback; instead, the service will simply be stopped."
So, all I got to do is to remove this line (android:stopWithTask="false") from the manifest and it worked just fine.
This is the updated code in the manifest file: