mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
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:
parent
fcac19737c
commit
dce909f00c
4 changed files with 15 additions and 17 deletions
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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?) {
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue