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.
This commit is contained in:
Luna712 2024-08-08 14:52:22 -06:00 committed by GitHub
parent fcac19737c
commit dce909f00c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 17 deletions

View file

@ -38,7 +38,7 @@ class DownloadChildFragment : Fragment() {
} }
override fun onDestroyView() { override fun onDestroyView() {
detachBackPressedCallback() activity?.detachBackPressedCallback()
binding = null binding = null
super.onDestroyView() super.onDestroyView()
} }
@ -113,7 +113,7 @@ class DownloadChildFragment : Fragment() {
adapter?.setIsMultiDeleteState(isMultiDeleteState) adapter?.setIsMultiDeleteState(isMultiDeleteState)
binding?.downloadDeleteAppbar?.isVisible = isMultiDeleteState binding?.downloadDeleteAppbar?.isVisible = isMultiDeleteState
if (!isMultiDeleteState) { if (!isMultiDeleteState) {
detachBackPressedCallback() activity?.detachBackPressedCallback()
downloadsViewModel.clearSelectedItems() downloadsViewModel.clearSelectedItems()
binding?.downloadChildToolbar?.isVisible = true binding?.downloadChildToolbar?.isVisible = true
} }

View file

@ -68,7 +68,7 @@ class DownloadFragment : Fragment() {
} }
override fun onDestroyView() { override fun onDestroyView() {
detachBackPressedCallback() activity?.detachBackPressedCallback()
binding = null binding = null
super.onDestroyView() super.onDestroyView()
} }
@ -160,7 +160,7 @@ class DownloadFragment : Fragment() {
adapter?.setIsMultiDeleteState(isMultiDeleteState) adapter?.setIsMultiDeleteState(isMultiDeleteState)
binding?.downloadDeleteAppbar?.isVisible = isMultiDeleteState binding?.downloadDeleteAppbar?.isVisible = isMultiDeleteState
if (!isMultiDeleteState) { if (!isMultiDeleteState) {
detachBackPressedCallback() activity?.detachBackPressedCallback()
downloadsViewModel.clearSelectedItems() downloadsViewModel.clearSelectedItems()
// Prevent race condition and make sure // Prevent race condition and make sure
// we don't display it early // we don't display it early

View file

@ -160,7 +160,7 @@ open class ResultTrailerPlayer : ResultFragmentPhone() {
activity?.attachBackPressedCallback { activity?.attachBackPressedCallback {
updateFullscreen(false) updateFullscreen(false)
} }
} else detachBackPressedCallback() } else activity?.detachBackPressedCallback()
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

View file

@ -2,29 +2,27 @@ package com.lagradost.cloudstream3.utils
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback import androidx.activity.OnBackPressedCallback
import java.util.WeakHashMap
object BackPressedCallbackHelper { object BackPressedCallbackHelper {
private var backPressedCallback: OnBackPressedCallback? = null private val backPressedCallbacks = WeakHashMap<ComponentActivity, OnBackPressedCallback>()
fun ComponentActivity.attachBackPressedCallback(callback: () -> Unit) { fun ComponentActivity.attachBackPressedCallback(callback: () -> Unit) {
if (backPressedCallback == null) { if (backPressedCallbacks[this] == null) {
backPressedCallback = object : OnBackPressedCallback(true) { val newCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() { override fun handleOnBackPressed() {
callback.invoke() callback.invoke()
} }
} }
backPressedCallbacks[this] = newCallback
onBackPressedDispatcher.addCallback(this, newCallback)
} }
backPressedCallback?.isEnabled = true backPressedCallbacks[this]?.isEnabled = true
onBackPressedDispatcher.addCallback(
this@attachBackPressedCallback,
backPressedCallback ?: return
)
} }
fun detachBackPressedCallback() { fun ComponentActivity.detachBackPressedCallback() {
backPressedCallback?.isEnabled = false backPressedCallbacks[this]?.isEnabled = false
backPressedCallback = null backPressedCallbacks.remove(this)
} }
} }