From 1ca9a52180ea93a96ba5f9a3221b727a2599282b Mon Sep 17 00:00:00 2001 From: Akshay Nandwana Date: Thu, 19 Dec 2024 10:21:43 +0530 Subject: [PATCH] Remove deprecated code (#2945) --- .../com/google/android/ground/MainActivity.kt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/ground/src/main/java/com/google/android/ground/MainActivity.kt b/ground/src/main/java/com/google/android/ground/MainActivity.kt index d085ad4460..cb973d30e8 100644 --- a/ground/src/main/java/com/google/android/ground/MainActivity.kt +++ b/ground/src/main/java/com/google/android/ground/MainActivity.kt @@ -19,6 +19,7 @@ import android.app.AlertDialog import android.content.Intent import android.os.Bundle import android.view.ViewGroup +import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AppCompatDelegate import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -85,6 +86,18 @@ class MainActivity : AbstractActivity() { lifecycleScope.launch { viewModel.navigationRequests.filterNotNull().collect { updateUi(binding.root, it) } } + + onBackPressedDispatcher.addCallback( + this, + object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + if (!dispatchBackPressed()) { + isEnabled = false + onBackPressedDispatcher.onBackPressed() + } + } + }, + ) } private fun updateUi(viewGroup: ViewGroup, uiState: MainUiState) { @@ -182,11 +195,6 @@ class MainActivity : AbstractActivity() { } } - @Deprecated("Deprecated in Java") - override fun onBackPressed() { - if (!dispatchBackPressed()) super.onBackPressed() - } - private fun dispatchBackPressed(): Boolean { val fragmentManager = navHostFragment.childFragmentManager val currentFragment = fragmentManager.findFragmentById(R.id.nav_host_fragment)