From 54dfa7bdd79f4d848bd5ed741bc2604c738fb150 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 27 Mar 2023 17:59:00 +0200 Subject: ui: cancel actionmode when pressing back button Signed-off-by: Jason A. Donenfeld --- .../java/com/wireguard/android/fragment/TunnelListFragment.kt | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'ui/src') diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt index 8928fb21..5f3d3950 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt @@ -16,6 +16,8 @@ import android.view.ViewGroup import android.view.animation.Animation import android.view.animation.AnimationUtils import android.widget.Toast +import androidx.activity.OnBackPressedCallback +import androidx.activity.addCallback import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ActionMode @@ -46,6 +48,7 @@ import kotlinx.coroutines.launch class TunnelListFragment : BaseFragment() { private val actionModeListener = ActionModeListener() private var actionMode: ActionMode? = null + private var backPressedCallback: OnBackPressedCallback? = null private var binding: TunnelListFragmentBinding? = null private val tunnelFileImportResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { data -> if (data == null) return@registerForActivityResult @@ -114,6 +117,9 @@ class TunnelListFragment : BaseFragment() { } executePendingBindings() } + backPressedCallback = requireActivity().onBackPressedDispatcher.addCallback(this) { actionMode?.finish() } + backPressedCallback?.isEnabled = false + return binding?.root } @@ -238,6 +244,7 @@ class TunnelListFragment : BaseFragment() { override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { actionMode = mode + backPressedCallback?.isEnabled = true if (activity != null) { resources = activity!!.resources } @@ -249,6 +256,7 @@ class TunnelListFragment : BaseFragment() { override fun onDestroyActionMode(mode: ActionMode) { actionMode = null + backPressedCallback?.isEnabled = false resources = null animateFab(binding?.createFab, true) checkedItems.clear() -- cgit v1.2.3