Bug 1918086 - Replace deprecated onBackPressed. r=android-reviewers,jonalmeida

Also delegate custom tab back handling to the browser fragment.

Differential Revision: https://phabricator.services.mozilla.com/D221829
This commit is contained in:
mcarare 2024-09-11 16:05:05 +00:00
Родитель afbb3979ab
Коммит 63292d7315
2 изменённых файлов: 22 добавлений и 12 удалений

Просмотреть файл

@ -8,6 +8,7 @@ import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import android.view.View
import androidx.activity.OnBackPressedCallback
import mozilla.components.browser.state.selector.findCustomTab
import mozilla.components.concept.engine.EngineView
import mozilla.components.support.locale.LocaleAwareAppCompatActivity
@ -62,6 +63,15 @@ class CustomTabActivity : LocaleAwareAppCompatActivity() {
startupTypeTelemetry = StartupTypeTelemetry(components.startupStateProvider, startupPathProvider).apply {
attachOnMainActivityOnCreate(lifecycle)
}
onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
browserFragment.onBackPressed()
}
},
)
}
override fun onPause() {
@ -72,15 +82,6 @@ class CustomTabActivity : LocaleAwareAppCompatActivity() {
}
}
@Suppress("MissingSuperCall", "OVERRIDE_DEPRECATION")
override fun onBackPressed() {
if (browserFragment.sessionFeature.onBackPressed()) {
return
} else {
onBackPressedDispatcher.onBackPressed()
}
}
override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View? {
return if (name == EngineView::class.java.name) {
val engineView = components.engine.createView(context, attrs)

Просмотреть файл

@ -15,6 +15,7 @@ import android.util.AttributeSet
import android.view.MenuItem
import android.view.View
import android.view.ViewTreeObserver
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.ActionBar
import androidx.appcompat.widget.Toolbar
@ -161,6 +162,15 @@ open class MainActivity : LocaleAwareAppCompatActivity() {
}
components.notificationsDelegate.bindToActivity(this)
onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
this@MainActivity.handleBackPressed()
}
},
)
}
private fun requestNotificationPermission() {
@ -334,8 +344,7 @@ open class MainActivity : LocaleAwareAppCompatActivity() {
}
}
@Suppress("MissingSuperCall", "OVERRIDE_DEPRECATION")
override fun onBackPressed() {
private fun handleBackPressed() {
val fragmentManager = supportFragmentManager
val urlInputFragment =
@ -369,7 +378,7 @@ open class MainActivity : LocaleAwareAppCompatActivity() {
return
}
onBackPressedDispatcher.onBackPressed()
super.onBackPressedDispatcher.onBackPressed()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {