[Android] Browser: Cancel adapter scope on destroy

Geoffrey Métais git at videolan.org
Wed Oct 30 16:46:40 CET 2019


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Oct 30 11:48:12 2019 +0100| [a53314f785da955c8cb3bdfc1bc5d2a3cf4db2cd] | committer: Geoffrey Métais

Browser: Cancel adapter scope on destroy

(cherry picked from commit 3697cede4cb8c9b6906b2eeec970b26d091beb69)

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

 vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt          | 2 +-
 .../src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt      | 5 +++++
 .../src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt    | 9 +++++----
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt
index ef7f809c6..14e505566 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt
@@ -10,7 +10,7 @@ import kotlinx.coroutines.channels.actor
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
 abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>(), CoroutineScope {
-    override val coroutineContext = Dispatchers.Main.immediate
+    override val coroutineContext = Dispatchers.Main.immediate + SupervisorJob()
 
     protected var dataset: List<D> = listOf()
     private set
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
index 10450b0cf..8aaa29b9b 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
@@ -181,6 +181,11 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
         viewModel.stop()
     }
 
+    override fun onDestroy() {
+        adapter.cancel()
+        super.onDestroy()
+    }
+
     override fun onSaveInstanceState(outState: Bundle) {
         outState.putString(KEY_MRL, mrl)
         outState.putParcelable(KEY_MEDIA, currentMedia)
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
index e39e85eb8..98db73252 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
@@ -27,7 +27,10 @@ import android.content.Context
 import android.net.Uri
 import android.view.View
 import androidx.databinding.ViewDataBinding
-import kotlinx.coroutines.*
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.ObsoleteCoroutinesApi
+import kotlinx.coroutines.launch
 import org.videolan.medialibrary.interfaces.AbstractMedialibrary
 import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 import org.videolan.medialibrary.media.MediaLibraryItem
@@ -51,7 +54,7 @@ internal class StorageBrowserAdapter(fragment: StorageBrowserFragment) : BaseBro
 
     override fun onBindViewHolder(holder: ViewHolder<ViewDataBinding>, position: Int) {
         val vh = holder as MediaViewHolder
-        vh.job = launch(start = CoroutineStart.UNDISPATCHED) {
+        vh.job = launch {
             var storage = getItem(position)
             if (storage.itemType == MediaLibraryItem.TYPE_MEDIA) storage = Storage((storage as AbstractMediaWrapper).uri)
             var storagePath = (storage as Storage).uri.path ?: ""
@@ -100,8 +103,6 @@ internal class StorageBrowserAdapter(fragment: StorageBrowserFragment) : BaseBro
             AbstractMedialibrary.getInstance().foldersList
         }
 
-
-
         folders.forEach {
             mediaDirsLocation.add(Uri.decode(if (it.startsWith("file://")) it.substring(7) else it))
         }



More information about the Android mailing list