onBackPressed -> onBackPressedDispatcher

This commit is contained in:
Abandoned Cart 2023-01-22 09:42:08 -05:00 committed by Niccolò Betto
parent bc3c49bc28
commit b20c6e9fc4
5 changed files with 32 additions and 17 deletions

View File

@ -12,6 +12,7 @@
<application <application
android:name=".SkylineApplication" android:name=".SkylineApplication"
android:enableOnBackInvokedCallback="true"
android:allowBackup="true" android:allowBackup="true"
android:extractNativeLibs="true" android:extractNativeLibs="true"
android:fullBackupContent="@xml/backup_descriptor" android:fullBackupContent="@xml/backup_descriptor"

View File

@ -15,6 +15,7 @@ import android.os.*
import android.util.Log import android.util.Log
import android.util.Rational import android.util.Rational
import android.view.* import android.view.*
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.view.isGone import androidx.core.view.isGone
@ -202,10 +203,6 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
emulationThread!!.start() emulationThread!!.start()
} }
override fun onBackPressed() {
returnFromEmulation()
}
@SuppressLint("SetTextI18n", "ClickableViewAccessibility") @SuppressLint("SetTextI18n", "ClickableViewAccessibility")
override fun onCreate(savedInstanceState : Bundle?) { override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -287,6 +284,16 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
changeAudioStatus(false) changeAudioStatus(false)
} }
override fun onStart() {
super.onStart()
onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
returnFromEmulation()
}
})
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()

View File

@ -11,6 +11,7 @@ import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.DocumentsContract import android.provider.DocumentsContract
import android.view.View import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -308,6 +309,23 @@ class MainActivity : AppCompatActivity() {
if (items.isEmpty()) adapter.setItems(listOf(HeaderViewItem(getString(R.string.no_rom)))) if (items.isEmpty()) adapter.setItems(listOf(HeaderViewItem(getString(R.string.no_rom))))
} }
override fun onStart() {
super.onStart()
onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
binding.searchBar.apply {
if (hasFocus() && text.isNotEmpty()) {
text = ""
clearFocus()
} else {
finish()
}
}
}
})
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
@ -330,15 +348,4 @@ class MainActivity : AppCompatActivity() {
adapter.notifyItemRangeChanged(0, adapter.currentItems.size) adapter.notifyItemRangeChanged(0, adapter.currentItems.size)
} }
} }
override fun onBackPressed() {
binding.searchBar.apply {
if (hasFocus() && text.isNotEmpty()) {
text = ""
clearFocus()
} else {
super.onBackPressed()
}
}
}
} }

View File

@ -145,7 +145,7 @@ class SettingsActivity : AppCompatActivity() {
*/ */
override fun onKeyUp(keyCode : Int, event : KeyEvent?) : Boolean { override fun onKeyUp(keyCode : Int, event : KeyEvent?) : Boolean {
if (keyCode == KeyEvent.KEYCODE_BUTTON_B) { if (keyCode == KeyEvent.KEYCODE_BUTTON_B) {
onBackPressed() onBackPressedDispatcher.onBackPressed()
return true return true
} }

View File

@ -342,7 +342,7 @@ class ControllerActivity : AppCompatActivity() {
*/ */
override fun onKeyUp(keyCode : Int, event : KeyEvent?) : Boolean { override fun onKeyUp(keyCode : Int, event : KeyEvent?) : Boolean {
if (keyCode == KeyEvent.KEYCODE_BUTTON_B) { if (keyCode == KeyEvent.KEYCODE_BUTTON_B) {
onBackPressed() onBackPressedDispatcher.onBackPressed()
return true return true
} }