diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2023-03-22 21:18:49 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2023-03-22 21:35:34 +0530 |
commit | b44380c81a17efc6ed52c0ca65b3adf2cac5637e (patch) | |
tree | 4f15d46130cb5eae64431677ea784d9003a8964d /ui/src/main/java | |
parent | 35c8c10b7e006c85f4fa20b1870fadb36107bade (diff) |
ui: migrate to MenuProvider
AndroidX Activity 1.4.0 introduced the new lifecycle-dependent MenuProvider
APIs that supersede existing onCreateOptionsMenu-style menus.
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'ui/src/main/java')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt | 19 | ||||
-rw-r--r-- | ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt | 20 |
2 files changed, 26 insertions, 13 deletions
diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt index 7a76edbe..8b155e24 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt @@ -8,9 +8,12 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.Menu import android.view.MenuInflater +import android.view.MenuItem import android.view.View import android.view.ViewGroup +import androidx.core.view.MenuProvider import androidx.databinding.DataBindingUtil +import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import com.wireguard.android.R import com.wireguard.android.backend.Tunnel @@ -24,18 +27,17 @@ import kotlinx.coroutines.launch /** * Fragment that shows details about a specific tunnel. */ -class TunnelDetailFragment : BaseFragment() { +class TunnelDetailFragment : BaseFragment(), MenuProvider { private var binding: TunnelDetailFragmentBinding? = null private var lastState = Tunnel.State.TOGGLE private var timerActive = true - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setHasOptionsMenu(true) + override fun onMenuItemSelected(menuItem: MenuItem): Boolean { + return false } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.tunnel_detail, menu) + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { + menuInflater.inflate(R.menu.tunnel_detail, menu) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, @@ -46,6 +48,11 @@ class TunnelDetailFragment : BaseFragment() { return binding?.root } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + requireActivity().addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED) + } + override fun onDestroyView() { binding = null super.onDestroyView() diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt index 315a6b86..c4c031fa 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt @@ -19,6 +19,8 @@ import android.view.WindowManager import android.view.inputmethod.InputMethodManager import android.widget.EditText import android.widget.Toast +import androidx.core.view.MenuProvider +import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import com.google.android.material.snackbar.Snackbar import com.wireguard.android.Application @@ -36,7 +38,7 @@ import kotlinx.coroutines.launch /** * Fragment for editing a WireGuard configuration. */ -class TunnelEditorFragment : BaseFragment() { +class TunnelEditorFragment : BaseFragment(), MenuProvider { private var haveShownKeys = false private var binding: TunnelEditorFragmentBinding? = null private var tunnel: ObservableTunnel? = null @@ -66,11 +68,10 @@ class TunnelEditorFragment : BaseFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setHasOptionsMenu(true) } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.config_editor, menu) + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { + menuInflater.inflate(R.menu.config_editor, menu) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, @@ -84,6 +85,11 @@ class TunnelEditorFragment : BaseFragment() { return binding?.root } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + requireActivity().addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED) + } + override fun onDestroyView() { activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) binding = null @@ -106,8 +112,8 @@ class TunnelEditorFragment : BaseFragment() { selectedTunnel = tunnel } - override fun onOptionsItemSelected(item: MenuItem): Boolean { - if (item.itemId == R.id.menu_action_save) { + override fun onMenuItemSelected(menuItem: MenuItem): Boolean { + if (menuItem.itemId == R.id.menu_action_save) { binding ?: return false val newConfig = try { binding!!.config!!.resolve() @@ -153,7 +159,7 @@ class TunnelEditorFragment : BaseFragment() { } return true } - return super.onOptionsItemSelected(item) + return false } @Suppress("UNUSED_PARAMETER") |