[Android] Fix leak on the PlaybackService flow in the BaseBrowserFragment

Nicolas Pomepuy git at videolan.org
Wed Oct 11 16:41:42 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Oct 10 14:26:53 2023 +0200| [1d760e30ea6876e72159e2202dc4bb1ec8a39345] | committer: Duncan McNamara

Fix leak on the PlaybackService flow in the BaseBrowserFragment

> https://code.videolan.org/videolan/vlc-android/commit/1d760e30ea6876e72159e2202dc4bb1ec8a39345
---

 .../src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt  | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
index 394f699279..dea76bb518 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
@@ -114,6 +114,7 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
     protected abstract fun browseRoot()
     private var needToRefreshMeta = false
     private var enqueuingSnackbar: Snackbar? = null
+    private lateinit var startedScope: CoroutineScope
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -128,9 +129,6 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
         }
         isRootDirectory = defineIsRoot()
         browserFavRepository = BrowserFavRepository.getInstance(requireContext())
-        PlaybackService.serviceFlow.onEach {
-            it?.addCallback(this)
-        }.launchIn(MainScope())
     }
 
     override fun onPrepareOptionsMenu(menu: Menu) {
@@ -232,6 +230,11 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
 
     override fun onStart() {
         super.onStart()
+        startedScope = MainScope()
+        PlaybackService.serviceFlow.onEach {
+            it?.addCallback(this)
+        }.launchIn(startedScope)
+
         fabPlay?.run {
             setImageResource(R.drawable.ic_fab_play)
             updateFab()
@@ -243,12 +246,13 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
 
     override fun onStop() {
         super.onStop()
+        startedScope.cancel()
+        PlaybackService.serviceFlow.value?.removeCallback(this)
         viewModel.stop()
     }
 
     override fun onDestroy() {
         if (::adapter.isInitialized) adapter.cancel()
-        PlaybackService.serviceFlow.value?.removeCallback(this)
         super.onDestroy()
     }
 



More information about the Android mailing list