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() {
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
}

View file

@ -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

View file

@ -160,7 +160,7 @@ open class ResultTrailerPlayer : ResultFragmentPhone() {
activity?.attachBackPressedCallback {
updateFullscreen(false)
}
} else detachBackPressedCallback()
} else activity?.detachBackPressedCallback()
}
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.OnBackPressedCallback
import java.util.WeakHashMap
object BackPressedCallbackHelper {
private var backPressedCallback: OnBackPressedCallback? = null
private val backPressedCallbacks = WeakHashMap<ComponentActivity, OnBackPressedCallback>()
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)
}
}