From 968c9fb4c1d85c01fb7e99152db7de224abc9edd Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 14 Aug 2023 16:28:29 +0200 Subject: [PATCH] Resolve onBackPressed deprecation --- .../nightscout/androidaps/MainActivity.kt | 65 ++++++++++--------- .../activities/PreferencesActivity.kt | 52 ++++++++------- .../activities/SingleFragmentActivity.kt | 43 ++++++------ .../activities/PrefImportListActivity.kt | 26 +++++--- .../setupwizard/SetupWizardActivity.kt | 17 +++-- .../sync/openhumans/ui/OHLoginActivity.kt | 40 +++++++----- .../activities/ComboV2PairingActivity.kt | 15 +++-- .../pump/eopatch/ui/EopatchActivity.kt | 20 +++--- .../pump/medtrum/ui/MedtrumActivity.kt | 14 +--- .../activity/OmnipodWizardActivityBase.kt | 12 +++- pump/pump-common/src/main/AndroidManifest.xml | 4 +- .../pump/common/ui/PumpBLEConfigActivity.kt | 28 +++++--- .../dialog/RileyLinkBLEConfigActivity.kt | 28 +++++--- .../ui/activities/TreatmentsActivity.kt | 28 ++++---- 14 files changed, 223 insertions(+), 169 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.kt b/app/src/main/java/info/nightscout/androidaps/MainActivity.kt index 2079a9a865..4d3cfc9ca6 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.kt @@ -21,6 +21,7 @@ import android.view.inputmethod.InputMethodManager import android.widget.EditText import android.widget.LinearLayout import android.widget.TextView +import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.widget.Toolbar import androidx.core.view.GravityCompat @@ -151,7 +152,7 @@ class MainActivity : DaggerAppCompatActivityWithResult() { .subscribe({ processPreferenceChange(it) }, fabricPrivacy::logException) if (startWizard() && !isRunningRealPumpTest()) { protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, { - startActivity(Intent(this, SetupWizardActivity::class.java)) + startActivity(Intent(this, SetupWizardActivity::class.java).setAction("info.nightscout.androidaps.MainActivity")) }) } androidPermission.notifyForStoragePermission(this) @@ -163,6 +164,17 @@ class MainActivity : DaggerAppCompatActivityWithResult() { androidPermission.notifyForBtConnectPermission(this) } passwordResetCheck(this) + onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + if (binding.mainDrawerLayout.isDrawerOpen(GravityCompat.START)) + binding.mainDrawerLayout.closeDrawers() + else if (menuOpen) + menu?.close() + else if (binding.mainPager.currentItem != 0) + binding.mainPager.currentItem = 0 + else finish() + } + }) } private fun checkPluginPreferences(viewPager: ViewPager2) { @@ -219,9 +231,11 @@ class MainActivity : DaggerAppCompatActivityWithResult() { menuItem.setIcon(info.nightscout.core.ui.R.drawable.ic_settings) } menuItem.setOnMenuItemClickListener { - val intent = Intent(this, SingleFragmentActivity::class.java) - intent.putExtra("plugin", activePlugin.getPluginsList().indexOf(p)) - startActivity(intent) + startActivity( + Intent(this, SingleFragmentActivity::class.java) + .setAction("info.nightscout.androidaps.MainActivity") + .putExtra("plugin", activePlugin.getPluginsList().indexOf(p)) + ) binding.mainDrawerLayout.closeDrawers() true } @@ -318,26 +332,28 @@ class MainActivity : DaggerAppCompatActivityWithResult() { when (item.itemId) { R.id.nav_preferences -> { protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, { - val i = Intent(this, PreferencesActivity::class.java) - i.putExtra("id", -1) - startActivity(i) + startActivity( + Intent(this, PreferencesActivity::class.java) + .setAction("info.nightscout.androidaps.MainActivity") + .putExtra("id", -1) + ) }) return true } R.id.nav_historybrowser -> { - startActivity(Intent(this, HistoryBrowseActivity::class.java)) + startActivity(Intent(this, HistoryBrowseActivity::class.java).setAction("info.nightscout.androidaps.MainActivity")) return true } R.id.nav_treatments -> { - startActivity(Intent(this, TreatmentsActivity::class.java)) + startActivity(Intent(this, TreatmentsActivity::class.java).setAction("info.nightscout.androidaps.MainActivity")) return true } R.id.nav_setupwizard -> { protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, { - startActivity(Intent(this, SetupWizardActivity::class.java)) + startActivity(Intent(this, SetupWizardActivity::class.java).setAction("info.nightscout.androidaps.MainActivity")) }) return true } @@ -384,9 +400,11 @@ class MainActivity : DaggerAppCompatActivityWithResult() { R.id.nav_plugin_preferences -> { val plugin = (binding.mainPager.adapter as TabPageAdapter).getPluginAt(binding.mainPager.currentItem) protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, { - val i = Intent(this, PreferencesActivity::class.java) - i.putExtra("id", plugin.preferencesId) - startActivity(i) + startActivity( + Intent(this, PreferencesActivity::class.java) + .setAction("info.nightscout.androidaps.MainActivity") + .putExtra("id", plugin.preferencesId) + ) }) return true } @@ -397,35 +415,18 @@ class MainActivity : DaggerAppCompatActivityWithResult() { } */ R.id.nav_defaultprofile -> { - startActivity(Intent(this, ProfileHelperActivity::class.java)) + startActivity(Intent(this, ProfileHelperActivity::class.java).setAction("info.nightscout.androidaps.MainActivity")) return true } R.id.nav_stats -> { - startActivity(Intent(this, StatsActivity::class.java)) + startActivity(Intent(this, StatsActivity::class.java).setAction("info.nightscout.androidaps.MainActivity")) return true } } return actionBarDrawerToggle.onOptionsItemSelected(item) } - override fun onBackPressed() { - if (binding.mainDrawerLayout.isDrawerOpen(GravityCompat.START)) { - binding.mainDrawerLayout.closeDrawers() - return - } - if (menuOpen) { - this.menu?.close() - return - } - if (binding.mainPager.currentItem != 0) { - binding.mainPager.currentItem = 0 - return - } - @Suppress("DEPRECATION") - super.onBackPressed() - } - // Correct place for calling setUserStats() would be probably MainApp // but we need to have it called at least once a day. Thus this location diff --git a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt index 7ea1d46f61..a6493ef61a 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt @@ -2,8 +2,10 @@ package info.nightscout.androidaps.activities import android.os.Bundle import android.view.Menu +import android.view.MenuInflater import android.view.MenuItem import androidx.appcompat.widget.SearchView +import androidx.core.view.MenuProvider import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceScreen import info.nightscout.androidaps.R @@ -33,25 +35,37 @@ class PreferencesActivity : DaggerAppCompatActivityWithResult(), PreferenceFragm it.putInt("id", preferenceId) } if (savedInstanceState == null) + @Suppress("CommitTransaction") supportFragmentManager.beginTransaction().replace(R.id.frame_layout, myPreferenceFragment!!).commit() - } - override fun onCreateOptionsMenu(menu: Menu): Boolean { - menuInflater.inflate(R.menu.menu_preferences, menu) - val searchItem = menu.findItem(R.id.menu_search) - searchView = searchItem.actionView as SearchView - searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener { + // Add menu items without overriding methods in the Activity + addMenuProvider(object : MenuProvider { + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { + // Add menu items here + menuInflater.inflate(R.menu.menu_preferences, menu) + val searchItem = menu.findItem(R.id.menu_search) + searchView = searchItem.actionView as SearchView + searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener { - override fun onQueryTextChange(newText: String): Boolean { - myPreferenceFragment?.setFilter(newText) - return false + override fun onQueryTextChange(newText: String): Boolean { + myPreferenceFragment?.setFilter(newText) + return false + } + + override fun onQueryTextSubmit(query: String): Boolean = false + }) } - override fun onQueryTextSubmit(query: String): Boolean { - return false - } + override fun onMenuItemSelected(menuItem: MenuItem): Boolean = + when (menuItem.itemId) { + android.R.id.home -> { + onBackPressedDispatcher.onBackPressed() + true + } + + else -> false + } }) - return super.onCreateOptionsMenu(menu) } override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat, pref: PreferenceScreen): Boolean { @@ -60,18 +74,8 @@ class PreferencesActivity : DaggerAppCompatActivityWithResult(), PreferenceFragm it.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, pref.key) it.putInt("id", preferenceId) } + @Suppress("CommitTransaction") supportFragmentManager.beginTransaction().replace(R.id.frame_layout, fragment, pref.key).addToBackStack(pref.key).commit() return true } - - override fun onOptionsItemSelected(item: MenuItem): Boolean = - when (item.itemId) { - android.R.id.home -> { - @Suppress("DEPRECATION") - onBackPressed() - true - } - - else -> super.onOptionsItemSelected(item) - } } \ No newline at end of file diff --git a/plugins/configuration/src/main/java/info/nightscout/configuration/activities/SingleFragmentActivity.kt b/plugins/configuration/src/main/java/info/nightscout/configuration/activities/SingleFragmentActivity.kt index cab20ee2e1..1e7e93ccbb 100644 --- a/plugins/configuration/src/main/java/info/nightscout/configuration/activities/SingleFragmentActivity.kt +++ b/plugins/configuration/src/main/java/info/nightscout/configuration/activities/SingleFragmentActivity.kt @@ -3,7 +3,9 @@ package info.nightscout.configuration.activities import android.content.Intent import android.os.Bundle import android.view.Menu +import android.view.MenuInflater import android.view.MenuItem +import androidx.core.view.MenuProvider import info.nightscout.configuration.R import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.plugin.PluginBase @@ -33,29 +35,32 @@ class SingleFragmentActivity : DaggerAppCompatActivityWithResult() { supportFragmentManager.fragmentFactory.instantiate(ClassLoader.getSystemClassLoader(), plugin?.pluginDescription?.fragmentClass!!) ).commit() } - } - override fun onOptionsItemSelected(item: MenuItem): Boolean = - when (item.itemId) { - android.R.id.home -> { - finish() - true + // Add menu items without overriding methods in the Activity + addMenuProvider(object : MenuProvider { + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { + if (plugin?.preferencesId != -1) menuInflater.inflate(R.menu.menu_single_fragment, menu) } - R.id.nav_plugin_preferences -> { - protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, { - val i = Intent(this, uiInteraction.preferencesActivity) - i.putExtra("id", plugin?.preferencesId) - startActivity(i) - }, null) - true - } + override fun onMenuItemSelected(menuItem: MenuItem): Boolean = + when (menuItem.itemId) { + android.R.id.home -> { + onBackPressedDispatcher.onBackPressed() + true + } - else -> super.onOptionsItemSelected(item) - } + R.id.nav_plugin_preferences -> { + protectionCheck.queryProtection(this@SingleFragmentActivity, ProtectionCheck.Protection.PREFERENCES, { + val i = Intent(this@SingleFragmentActivity, uiInteraction.preferencesActivity) + .setAction("info.nightscout.configuration.activities.SingleFragmentActivity") + .putExtra("id", plugin?.preferencesId) + startActivity(i) + }, null) + true + } - override fun onCreateOptionsMenu(menu: Menu): Boolean { - if (plugin?.preferencesId != -1) menuInflater.inflate(R.menu.menu_single_fragment, menu) - return super.onCreateOptionsMenu(menu) + else -> false + } + }) } } \ No newline at end of file diff --git a/plugins/configuration/src/main/java/info/nightscout/configuration/maintenance/activities/PrefImportListActivity.kt b/plugins/configuration/src/main/java/info/nightscout/configuration/maintenance/activities/PrefImportListActivity.kt index 27ec178c8d..acee12e375 100644 --- a/plugins/configuration/src/main/java/info/nightscout/configuration/maintenance/activities/PrefImportListActivity.kt +++ b/plugins/configuration/src/main/java/info/nightscout/configuration/maintenance/activities/PrefImportListActivity.kt @@ -3,9 +3,12 @@ package info.nightscout.configuration.maintenance.activities import android.content.Intent 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.fragment.app.FragmentActivity import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -41,6 +44,21 @@ class PrefImportListActivity : TranslatedDaggerAppCompatActivity() { binding.recyclerview.layoutManager = LinearLayoutManager(this) binding.recyclerview.adapter = RecyclerViewAdapter(prefFileListProvider.listPreferenceFiles(loadMetadata = true)) + + // Add menu items without overriding methods in the Activity + addMenuProvider(object : MenuProvider { + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {} + + override fun onMenuItemSelected(menuItem: MenuItem): Boolean = + when (menuItem.itemId) { + android.R.id.home -> { + onBackPressedDispatcher.onBackPressed() + true + } + + else -> false + } + }) } inner class RecyclerViewAdapter internal constructor(private var prefFileList: List) : RecyclerView.Adapter() { @@ -106,12 +124,4 @@ class PrefImportListActivity : TranslatedDaggerAppCompatActivity() { } } } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - if (item.itemId == android.R.id.home) { - finish() - return true - } - return super.onOptionsItemSelected(item) - } } \ No newline at end of file diff --git a/plugins/configuration/src/main/java/info/nightscout/configuration/setupwizard/SetupWizardActivity.kt b/plugins/configuration/src/main/java/info/nightscout/configuration/setupwizard/SetupWizardActivity.kt index a3ae79c65a..0bd2c50e21 100644 --- a/plugins/configuration/src/main/java/info/nightscout/configuration/setupwizard/SetupWizardActivity.kt +++ b/plugins/configuration/src/main/java/info/nightscout/configuration/setupwizard/SetupWizardActivity.kt @@ -4,6 +4,7 @@ import android.content.Intent import android.os.Bundle import android.view.View import android.widget.TextView +import androidx.activity.OnBackPressedCallback import dagger.android.HasAndroidInjector import info.nightscout.configuration.R import info.nightscout.configuration.activities.DaggerAppCompatActivityWithResult @@ -64,6 +65,12 @@ class SetupWizardActivity : DaggerAppCompatActivityWithResult() { generateLayout() updateButtons() } + onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + if (currentWizardPage == 0) + OKDialog.showConfirmation(this@SetupWizardActivity, rh.gs(R.string.exitwizard)) { finish() } else showPreviousPage(null) + } + }) } override fun onPause() { @@ -134,10 +141,6 @@ class SetupWizardActivity : DaggerAppCompatActivityWithResult() { } } - override fun onBackPressed() { - if (currentWizardPage == 0) OKDialog.showConfirmation(this, rh.gs(R.string.exitwizard)) { finish() } else showPreviousPage(null) - } - @Suppress("UNUSED_PARAMETER") fun exitPressed(view: View?) { sp.putBoolean(R.string.key_setupwizard_processed, true) @@ -147,7 +150,7 @@ class SetupWizardActivity : DaggerAppCompatActivityWithResult() { @Suppress("UNUSED_PARAMETER") fun showNextPage(view: View?) { finish() - val intent = Intent(this, SetupWizardActivity::class.java) + val intent = Intent(this, SetupWizardActivity::class.java).setAction("info.nightscout.configuration.setupwizard.SetupWizardActivity") intent.putExtra(intentMessage, nextPage(null)) startActivity(intent) } @@ -155,7 +158,7 @@ class SetupWizardActivity : DaggerAppCompatActivityWithResult() { @Suppress("UNUSED_PARAMETER") fun showPreviousPage(view: View?) { finish() - val intent = Intent(this, SetupWizardActivity::class.java) + val intent = Intent(this, SetupWizardActivity::class.java).setAction("info.nightscout.configuration.setupwizard.SetupWizardActivity") intent.putExtra(intentMessage, previousPage(null)) startActivity(intent) } @@ -164,7 +167,7 @@ class SetupWizardActivity : DaggerAppCompatActivityWithResult() { @Suppress("UNUSED_PARAMETER") fun finishSetupWizard(view: View?) { sp.putBoolean(R.string.key_setupwizard_processed, true) - val intent = Intent(this, uiInteraction.mainActivity) + val intent = Intent(this, uiInteraction.mainActivity).setAction("info.nightscout.configuration.setupwizard.SetupWizardActivity") intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) startActivity(intent) finish() diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/openhumans/ui/OHLoginActivity.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/openhumans/ui/OHLoginActivity.kt index 1dc79a3912..b7df7d81d8 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/openhumans/ui/OHLoginActivity.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/openhumans/ui/OHLoginActivity.kt @@ -3,11 +3,15 @@ package info.nightscout.plugins.sync.openhumans.ui import android.content.Intent import android.net.Uri import android.os.Bundle +import android.view.Menu +import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.widget.CheckBox +import androidx.activity.OnBackPressedCallback import androidx.activity.viewModels import androidx.browser.customtabs.CustomTabsIntent +import androidx.core.view.MenuProvider import androidx.core.view.ViewCompat import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat @@ -94,6 +98,25 @@ class OHLoginActivity : TranslatedDaggerAppCompatActivity() { if (code != null) { viewModel.submitBearerToken(code) } + // Add menu items without overriding methods in the Activity + addMenuProvider(object : MenuProvider { + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {} + + override fun onMenuItemSelected(menuItem: MenuItem): Boolean = + when (menuItem.itemId) { + android.R.id.home -> { + onBackPressedDispatcher.onBackPressed() + true + } + + else -> false + } + }) + onBackPressedDispatcher.addCallback(this, object: OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + if (!viewModel.goBack()) finish() + } + }) } override fun onNewIntent(intent: Intent) { @@ -103,21 +126,4 @@ class OHLoginActivity : TranslatedDaggerAppCompatActivity() { viewModel.submitBearerToken(code) } } - - override fun onBackPressed() { - if (!viewModel.goBack()) { - @Suppress("DEPRECATION") - super.onBackPressed() - } - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean = - if (item.itemId == android.R.id.home) { - @Suppress("DEPRECATION") - onBackPressed() - true - } else { - super.onOptionsItemSelected(item) - } - } \ No newline at end of file diff --git a/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/activities/ComboV2PairingActivity.kt b/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/activities/ComboV2PairingActivity.kt index 48e7158d1e..cc5c220f89 100644 --- a/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/activities/ComboV2PairingActivity.kt +++ b/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/activities/ComboV2PairingActivity.kt @@ -9,6 +9,7 @@ import android.text.Editable import android.text.TextWatcher import android.view.View import androidx.activity.ComponentActivity +import androidx.activity.OnBackPressedCallback import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts import androidx.core.content.ContextCompat @@ -186,13 +187,13 @@ class ComboV2PairingActivity : TranslatedDaggerAppCompatActivity() { .launchIn(this) } } - } - - override fun onBackPressed() { - aapsLogger.info(LTag.PUMP, "User pressed the back button; cancelling any ongoing pairing") - combov2Plugin.cancelPairing() - @Suppress("DEPRECATION") - super.onBackPressed() + onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + aapsLogger.info(LTag.PUMP, "User pressed the back button; cancelling any ongoing pairing") + combov2Plugin.cancelPairing() + finish() + } + }) } override fun onDestroy() { diff --git a/pump/eopatch/src/main/java/info/nightscout/androidaps/plugins/pump/eopatch/ui/EopatchActivity.kt b/pump/eopatch/src/main/java/info/nightscout/androidaps/plugins/pump/eopatch/ui/EopatchActivity.kt index 1f9cc7f15d..f30e06a5ff 100644 --- a/pump/eopatch/src/main/java/info/nightscout/androidaps/plugins/pump/eopatch/ui/EopatchActivity.kt +++ b/pump/eopatch/src/main/java/info/nightscout/androidaps/plugins/pump/eopatch/ui/EopatchActivity.kt @@ -6,6 +6,7 @@ import android.content.Intent import android.content.pm.ActivityInfo import android.os.Bundle import android.view.MotionEvent +import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AlertDialog import androidx.lifecycle.ViewModelProvider import info.nightscout.androidaps.plugins.pump.eopatch.R @@ -103,6 +104,16 @@ class EopatchActivity : EoBaseActivity() { } } } + onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + binding.viewModel?.apply { + when (patchStep.value) { + PatchStep.SAFE_DEACTIVATION -> this@EopatchActivity.finish() + else -> Unit + } + } + } + }) } override fun onNewIntent(intent: Intent?) { @@ -274,15 +285,6 @@ class EopatchActivity : EoBaseActivity() { this@EopatchActivity.finish() } - override fun onBackPressed() { - binding.viewModel?.apply{ - when(patchStep.value){ - PatchStep.SAFE_DEACTIVATION -> this@EopatchActivity.finish() - else -> Unit - } - } - } - companion object { const val RESULT_DISCARDED = RESULT_FIRST_USER + 1 const val EXTRA_START_PATCH_STEP = "EXTRA_START_PATCH_FRAGMENT_UI" diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumActivity.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumActivity.kt index a7fd32336f..683da88042 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumActivity.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumActivity.kt @@ -29,7 +29,7 @@ class MedtrumActivity : MedtrumBaseActivity() { window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) binding.apply { - viewModel = ViewModelProvider(this@MedtrumActivity, viewModelFactory).get(MedtrumViewModel::class.java) + viewModel = ViewModelProvider(this@MedtrumActivity, viewModelFactory)[MedtrumViewModel::class.java] viewModel?.apply { processIntent(intent) @@ -88,20 +88,10 @@ class MedtrumActivity : MedtrumBaseActivity() { } } - override fun onDestroy() { - super.onDestroy() - } - - override fun onBackPressed() { - binding.viewModel?.apply { - // Do nothing - } - } - companion object { const val EXTRA_START_PATCH_STEP = "EXTRA_START_PATCH_FRAGMENT_UI" - const val EXTRA_START_FROM_MENU = "EXTRA_START_FROM_MENU" + private const val EXTRA_START_FROM_MENU = "EXTRA_START_FROM_MENU" @JvmStatic fun createIntentFromMenu(context: Context, patchStep: PatchStep): Intent { return Intent(context, MedtrumActivity::class.java).apply { diff --git a/pump/omnipod-common/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/common/ui/wizard/common/activity/OmnipodWizardActivityBase.kt b/pump/omnipod-common/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/common/ui/wizard/common/activity/OmnipodWizardActivityBase.kt index 7b56898a59..1399ad58a8 100644 --- a/pump/omnipod-common/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/common/ui/wizard/common/activity/OmnipodWizardActivityBase.kt +++ b/pump/omnipod-common/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/common/ui/wizard/common/activity/OmnipodWizardActivityBase.kt @@ -1,5 +1,8 @@ package info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.common.activity +import android.os.Bundle +import android.os.PersistableBundle +import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AlertDialog import androidx.navigation.NavController import androidx.navigation.fragment.NavHostFragment @@ -8,7 +11,14 @@ import info.nightscout.core.ui.activities.TranslatedDaggerAppCompatActivity abstract class OmnipodWizardActivityBase : TranslatedDaggerAppCompatActivity() { - override fun onBackPressed() = exitActivityAfterConfirmation() + override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { + super.onCreate(savedInstanceState, persistentState) + onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + exitActivityAfterConfirmation() + } + }) + } fun exitActivityAfterConfirmation() { if (getNavController().previousBackStackEntry == null) { diff --git a/pump/pump-common/src/main/AndroidManifest.xml b/pump/pump-common/src/main/AndroidManifest.xml index 43a77bf6e2..c5b155f377 100644 --- a/pump/pump-common/src/main/AndroidManifest.xml +++ b/pump/pump-common/src/main/AndroidManifest.xml @@ -2,11 +2,11 @@ - + diff --git a/pump/pump-common/src/main/java/info/nightscout/pump/common/ui/PumpBLEConfigActivity.kt b/pump/pump-common/src/main/java/info/nightscout/pump/common/ui/PumpBLEConfigActivity.kt index 68c6f30eed..42928f570f 100644 --- a/pump/pump-common/src/main/java/info/nightscout/pump/common/ui/PumpBLEConfigActivity.kt +++ b/pump/pump-common/src/main/java/info/nightscout/pump/common/ui/PumpBLEConfigActivity.kt @@ -13,6 +13,8 @@ import android.content.Context import android.os.Bundle import android.os.Handler import android.os.HandlerThread +import android.view.Menu +import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup @@ -20,6 +22,7 @@ import android.widget.AdapterView import android.widget.AdapterView.OnItemClickListener import android.widget.BaseAdapter import android.widget.TextView +import androidx.core.view.MenuProvider import info.nightscout.core.ui.activities.TranslatedDaggerAppCompatActivity import info.nightscout.core.ui.dialogs.OKDialog import info.nightscout.interfaces.plugin.ActivePlugin @@ -140,6 +143,21 @@ class PumpBLEConfigActivity : TranslatedDaggerAppCompatActivity() { updateCurrentlySelectedBTDevice() }) } + + // Add menu items without overriding methods in the Activity + addMenuProvider(object : MenuProvider { + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {} + + override fun onMenuItemSelected(menuItem: MenuItem): Boolean = + when (menuItem.itemId) { + android.R.id.home -> { + onBackPressedDispatcher.onBackPressed() + true + } + + else -> false + } + }) } private fun updateCurrentlySelectedBTDevice() { @@ -156,16 +174,6 @@ class PumpBLEConfigActivity : TranslatedDaggerAppCompatActivity() { } } - override fun onOptionsItemSelected(item: MenuItem): Boolean = - when (item.itemId) { - android.R.id.home -> { - finish() - true - } - - else -> super.onOptionsItemSelected(item) - } - override fun onResume() { super.onResume() bleSelector.onResume() diff --git a/pump/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/dialog/RileyLinkBLEConfigActivity.kt b/pump/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/dialog/RileyLinkBLEConfigActivity.kt index a9d0e976b3..da05125412 100644 --- a/pump/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/dialog/RileyLinkBLEConfigActivity.kt +++ b/pump/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/dialog/RileyLinkBLEConfigActivity.kt @@ -17,6 +17,8 @@ import android.os.Bundle import android.os.Handler import android.os.HandlerThread import android.os.ParcelUuid +import android.view.Menu +import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup @@ -26,6 +28,7 @@ import android.widget.BaseAdapter import android.widget.TextView import android.widget.Toast import androidx.core.app.ActivityCompat +import androidx.core.view.MenuProvider import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.R import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil @@ -118,6 +121,21 @@ class RileyLinkBLEConfigActivity : TranslatedDaggerAppCompatActivity() { updateCurrentlySelectedRileyLink() }) } + + // Add menu items without overriding methods in the Activity + addMenuProvider(object : MenuProvider { + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {} + + override fun onMenuItemSelected(menuItem: MenuItem): Boolean = + when (menuItem.itemId) { + android.R.id.home -> { + onBackPressedDispatcher.onBackPressed() + true + } + + else -> false + } + }) } private fun updateCurrentlySelectedRileyLink() { @@ -148,16 +166,6 @@ class RileyLinkBLEConfigActivity : TranslatedDaggerAppCompatActivity() { } } - override fun onOptionsItemSelected(item: MenuItem): Boolean = - when (item.itemId) { - android.R.id.home -> { - finish() - true - } - - else -> super.onOptionsItemSelected(item) - } - private fun prepareForScanning() { val checkOK = blePreCheck.prerequisitesCheck(this) if (checkOK) { diff --git a/ui/src/main/java/info/nightscout/ui/activities/TreatmentsActivity.kt b/ui/src/main/java/info/nightscout/ui/activities/TreatmentsActivity.kt index f53797d513..ee79248e3f 100644 --- a/ui/src/main/java/info/nightscout/ui/activities/TreatmentsActivity.kt +++ b/ui/src/main/java/info/nightscout/ui/activities/TreatmentsActivity.kt @@ -1,7 +1,10 @@ package info.nightscout.ui.activities import android.os.Bundle +import android.view.Menu +import android.view.MenuInflater import android.view.MenuItem +import androidx.core.view.MenuProvider import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction import com.google.android.material.tabs.TabLayout @@ -61,23 +64,26 @@ class TreatmentsActivity : TranslatedDaggerAppCompatActivity() { override fun onTabUnselected(tab: TabLayout.Tab) {} override fun onTabReselected(tab: TabLayout.Tab) {} }) + + addMenuProvider(object : MenuProvider { + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {} + + override fun onMenuItemSelected(menuItem: MenuItem): Boolean = + when (menuItem.itemId) { + android.R.id.home -> { + onBackPressedDispatcher.onBackPressed() + true + } + + else -> false + } + }) } - override fun onOptionsItemSelected(item: MenuItem): Boolean = - when (item.itemId) { - android.R.id.home -> { - finish() - true - } - - else -> super.onOptionsItemSelected(item) - } - private fun setFragment(selectedFragment: Fragment) { supportFragmentManager.beginTransaction() .replace(R.id.fragment_container, selectedFragment) .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .commit() } - } \ No newline at end of file