From 93546382b9de6fa30ed7337a0eeb070695aa6199 Mon Sep 17 00:00:00 2001 From: Krunal Patel Date: Wed, 30 Nov 2022 11:02:19 +0530 Subject: [PATCH] Fix service not stopping on close video or removed from recents Stop the `BackgroundMode` service when close icon on player is pressed. Stop the `BackgroundMode` service when app is removed by user from task manager. --- .../java/com/github/libretube/services/BackgroundMode.kt | 8 ++++++++ .../com/github/libretube/ui/fragments/PlayerFragment.kt | 2 ++ 2 files changed, 10 insertions(+) diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index 719d523cd..c44ea4f8d 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -353,6 +353,14 @@ class BackgroundMode : Service() { } } + /** + * Stop the service when app is removed from the task manager. + */ + override fun onTaskRemoved(rootIntent: Intent?) { + super.onTaskRemoved(rootIntent) + onDestroy() + } + /** * destroy the [BackgroundMode] foreground service */ diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 765820222..d7b87a6ad 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -303,6 +303,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { mainActivity.supportFragmentManager.beginTransaction() .remove(this) .commit() + BackgroundHelper.stopBackgroundPlay(requireContext()) } playerBinding.closeImageButton.setOnClickListener { viewModel.isFullscreen.value = false @@ -311,6 +312,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { mainActivity.supportFragmentManager.beginTransaction() .remove(this) .commit() + BackgroundHelper.stopBackgroundPlay(requireContext()) } binding.playImageView.setOnClickListener {