From dce909f00cbbc3d9dfeeee78a48dcbf297329bb9 Mon Sep 17 00:00:00 2001 From: Luna712 <142361265+Luna712@users.noreply.github.com> Date: Thu, 8 Aug 2024 14:52:22 -0600 Subject: [PATCH] Fix BackPressedCallback conflict between activities There is another, more simple way to do this by just detatching the callback everytime it is pressed in DownloadedPlayerActivity, however, I figured this method was more future-proofed if more activities are added later so went with this, but if you want me to go to do the more simple method I can also. --- .../ui/download/DownloadChildFragment.kt | 4 ++-- .../ui/download/DownloadFragment.kt | 4 ++-- .../ui/result/ResultTrailerPlayer.kt | 2 +- .../utils/BackPressedCallbackHelper.kt | 22 +++++++++---------- 4 files changed, 15 insertions(+), 17 deletions(-) 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