ui: cancel actionmode when pressing back button

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-03-27 17:59:00 +02:00
parent e5a75307c2
commit 54dfa7bdd7

View File

@ -16,6 +16,8 @@ import android.view.ViewGroup
import android.view.animation.Animation import android.view.animation.Animation
import android.view.animation.AnimationUtils import android.view.animation.AnimationUtils
import android.widget.Toast import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.ActionMode import androidx.appcompat.view.ActionMode
@ -46,6 +48,7 @@ import kotlinx.coroutines.launch
class TunnelListFragment : BaseFragment() { class TunnelListFragment : BaseFragment() {
private val actionModeListener = ActionModeListener() private val actionModeListener = ActionModeListener()
private var actionMode: ActionMode? = null private var actionMode: ActionMode? = null
private var backPressedCallback: OnBackPressedCallback? = null
private var binding: TunnelListFragmentBinding? = null private var binding: TunnelListFragmentBinding? = null
private val tunnelFileImportResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { data -> private val tunnelFileImportResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { data ->
if (data == null) return@registerForActivityResult if (data == null) return@registerForActivityResult
@ -114,6 +117,9 @@ class TunnelListFragment : BaseFragment() {
} }
executePendingBindings() executePendingBindings()
} }
backPressedCallback = requireActivity().onBackPressedDispatcher.addCallback(this) { actionMode?.finish() }
backPressedCallback?.isEnabled = false
return binding?.root return binding?.root
} }
@ -238,6 +244,7 @@ class TunnelListFragment : BaseFragment() {
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
actionMode = mode actionMode = mode
backPressedCallback?.isEnabled = true
if (activity != null) { if (activity != null) {
resources = activity!!.resources resources = activity!!.resources
} }
@ -249,6 +256,7 @@ class TunnelListFragment : BaseFragment() {
override fun onDestroyActionMode(mode: ActionMode) { override fun onDestroyActionMode(mode: ActionMode) {
actionMode = null actionMode = null
backPressedCallback?.isEnabled = false
resources = null resources = null
animateFab(binding?.createFab, true) animateFab(binding?.createFab, true)
checkedItems.clear() checkedItems.clear()