diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt index 09c48a04..d5c463df 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt @@ -38,7 +38,7 @@ class DownloadChildFragment : Fragment() { } override fun onDestroyView() { - detachBackPressedCallback() + activity?.detachBackPressedCallback() binding = null super.onDestroyView() } @@ -113,7 +113,7 @@ class DownloadChildFragment : Fragment() { adapter?.setIsMultiDeleteState(isMultiDeleteState) binding?.downloadDeleteAppbar?.isVisible = isMultiDeleteState if (!isMultiDeleteState) { - detachBackPressedCallback() + activity?.detachBackPressedCallback() downloadsViewModel.clearSelectedItems() binding?.downloadChildToolbar?.isVisible = true } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadFragment.kt index 447b4f13..dfa7635c 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadFragment.kt @@ -68,7 +68,7 @@ class DownloadFragment : Fragment() { } override fun onDestroyView() { - detachBackPressedCallback() + activity?.detachBackPressedCallback() binding = null super.onDestroyView() } @@ -160,7 +160,7 @@ class DownloadFragment : Fragment() { adapter?.setIsMultiDeleteState(isMultiDeleteState) binding?.downloadDeleteAppbar?.isVisible = isMultiDeleteState if (!isMultiDeleteState) { - detachBackPressedCallback() + activity?.detachBackPressedCallback() downloadsViewModel.clearSelectedItems() // Prevent race condition and make sure // we don't display it early diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt index 2ab60c2f..9701a980 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt @@ -160,7 +160,7 @@ open class ResultTrailerPlayer : ResultFragmentPhone() { activity?.attachBackPressedCallback { updateFullscreen(false) } - } else detachBackPressedCallback() + } else activity?.detachBackPressedCallback() } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackPressedCallbackHelper.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackPressedCallbackHelper.kt index 1326ab27..abb3e2a2 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackPressedCallbackHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackPressedCallbackHelper.kt @@ -2,29 +2,27 @@ package com.lagradost.cloudstream3.utils import androidx.activity.ComponentActivity import androidx.activity.OnBackPressedCallback +import java.util.WeakHashMap object BackPressedCallbackHelper { - private var backPressedCallback: OnBackPressedCallback? = null + private val backPressedCallbacks = WeakHashMap() fun ComponentActivity.attachBackPressedCallback(callback: () -> Unit) { - if (backPressedCallback == null) { - backPressedCallback = object : OnBackPressedCallback(true) { + if (backPressedCallbacks[this] == null) { + val newCallback = object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { callback.invoke() } } + backPressedCallbacks[this] = newCallback + onBackPressedDispatcher.addCallback(this, newCallback) } - backPressedCallback?.isEnabled = true - - onBackPressedDispatcher.addCallback( - this@attachBackPressedCallback, - backPressedCallback ?: return - ) + backPressedCallbacks[this]?.isEnabled = true } - fun detachBackPressedCallback() { - backPressedCallback?.isEnabled = false - backPressedCallback = null + fun ComponentActivity.detachBackPressedCallback() { + backPressedCallbacks[this]?.isEnabled = false + backPressedCallbacks.remove(this) } } \ No newline at end of file