onTaskRemoved not called when app dismissed from recent tasks on Android 14
Version
Media3 1.1.1
More version details
Also happens with 1.2.0rc-1
Devices that reproduce the issue
- API 34 emulator (Google Play)
- Pixel 6a with Android 14
Devices that do not reproduce the issue
- API 33 emulator (Google APIs)
- Samsung Tab S7+ with Android 13
Reproducible in the demo app?
Yes
Reproduction steps
- Play some audio in demo-session app
- Tap pause in the app
- Navigate to the home screen
- Swipe up on the demo-session app from recent apps
- Observe dead notification in notifications
Expected result
There is no notification in the notification drawer
Actual result
There is a notification in the notification drawer that is inoperable, if you click on play button, a DeadObjectException is thrown
Media
Jazz & Blues, Jazz in Paris
Bug Report
- [ ] You will email the zip file produced by
adb bugreportto [email protected] after filing this issue.
https://github.com/androidx/media/assets/5206058/90f40abb-737e-4ea5-9cdf-36ef69a987d2
Here's video of the bug occurring, perhaps an important reproduction step is you have to navigate back to the home screen before going to the recent apps list, otherwise it doesn't seem to occur
Yeah, true. I can repro this. Took a while to make it repro but I was able to see that twice now. Thanks for reporting. We look into this.
This is a bug in Android 14. We have reported this internally and a fix is on it's way. There is nothing Media3 as a library or an app could do about it.
~~It has been fixed to the security patch version of 2024-02-05, Android14, Pixel6~~
It's my mistake. sorry. This bug still exists.
Thanks for the heads up. Can you give us the build number with which you have verified this has been fixed and how you tried to repro?
When I test with UQ1A.240205.004 I still see the problem I'm afraid.
@svenoaks @marcbaechinger I'm facing this issue on Android 13 as well. The work around that I have implemented as @marcbaechinger described in one of the comments is to release the player and session before calling stopSelf in onTaskRemoved. I just wanted to confirm whether this issue is only occurring when player is paused ? as when player is playing and I remove app from recents, it works fine.
I think these are two different issues.
On Android 14 and with the repro steps from above, onTaskRemoved isn't called at all.
I just wanted to confirm whether this issue is only occurring when player is paused ? as when player is playing and I remove app from recents, it works fine.
Pausing the player to repro this issue is required, because that is how the demo app is implemented. Without pausing, playback would continue in the background when dismissing the app from recents which is working as intended.
Generally, to stop the service the service must not run in the foreground (hence the player needs to be paused) and no controller/browser must be bound to the service (options: all controller disconnected already or the session is released). If either of these conditions are violated, calling stopSelf is a no-op.
@marcbaechinger thanks for the clarity. the two conditions that you mention about stopping the service make sense. In my case, even on paused state, on dismissing the app doesn't remove the notification implies that any controller is not disconnected yet ? then trying to play the media from notification results in ForegroundServiceDidNotStartInTimeException.
I'd guess so yes, but I can't know with the available information. #1059 is about the same thing and may be interesting for you.
I was able to work around this issue with this code (simplified version):
handler.post {
if (willDie) {
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
throw NullPointerException("this code path should never be reachable")
}
// Android 14 bug.
// https://github.com/androidx/media/issues/805
// In Android 13:
// 1. Service starts
// 2. UI thread moves on to this call
// 3. Some time later, user swipes away app (while music paused)
// 4. onTaskRemoved() calls stopSelf()
// 5. onDestroy() called, happy ending
// In Android 14:
// 1. Service starts
// 2. UI thread moves on to this call
// 3. Some time later, user swipes away app (while music paused)
// 4. App process killed, notification abandoned
// 5. App process restarted
// 6. instantly, onTaskRemoved() calls stopSelf()
// 7. This is called (and because we are after onTaskRemoved, willDie is true)
// 8. onDestroy() called
// To workaround this, we cancel notification here and let service die peacefully
val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
nm.cancel(DefaultMediaNotificationProvider.DEFAULT_NOTIFICATION_ID)
return@post
}
}
in onCreate()
and this in onTaskRemoved():
override fun onTaskRemoved(rootIntent: Intent?) {
if (!mediaSession!!.player.playWhenReady || mediaSession!!.player.mediaItemCount == 0) {
willDie = true // a14 bug workaround
stopSelf()
}
}