Update deprecated onBackPressed, support predictive back gesture (#2959)

- Update onBackPressed functions which have been deprecated to use the new OnBackPressedDispatcher and OnBackPressedCallback
 - Indicate support for the predictive back gesture
This commit is contained in:
Joris Pelgröm 2022-10-19 20:49:49 +02:00 committed by GitHub
parent 51f9a3fe68
commit 47e8957a92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 18 deletions

View file

@ -52,9 +52,8 @@ class SettingsWearMainView : AppCompatActivity() {
LoadSettingsHomeView(
settingsWearViewModel,
currentNodes.firstOrNull()?.displayName ?: "unknown",
this::loginWearOs,
this::onBackPressed
)
this::loginWearOs
) { onBackPressedDispatcher.onBackPressed() }
}
if (registerUrl != null) {

View file

@ -65,6 +65,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.HomeAssistant"
android:networkSecurityConfig="@xml/network_security_config"
android:enableOnBackInvokedCallback="true"
tools:ignore="GoogleAppIndexingWarning">
<!-- Start things like SensorWorker on device boot -->

View file

@ -3,6 +3,7 @@ package io.homeassistant.companion.android.onboarding
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import androidx.activity.OnBackPressedCallback
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationManagerCompat
@ -68,13 +69,15 @@ class OnboardingActivity : AppCompatActivity() {
}
}
}
}
override fun onBackPressed() {
if (supportFragmentManager.backStackEntryCount > 0) {
supportFragmentManager.popBackStack()
} else {
super.onBackPressed()
val onBackPressed = object : OnBackPressedCallback(supportFragmentManager.backStackEntryCount > 0) {
override fun handleOnBackPressed() {
supportFragmentManager.popBackStack()
}
}
onBackPressedDispatcher.addCallback(this, onBackPressed)
supportFragmentManager.addOnBackStackChangedListener {
onBackPressed.isEnabled = supportFragmentManager.backStackEntryCount > 0
}
}

View file

@ -80,7 +80,7 @@ class SettingsActivity : BaseActivity() {
if (supportFragmentManager.backStackEntryCount > 0) {
supportFragmentManager.popBackStack()
} else {
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
true
}

View file

@ -42,6 +42,7 @@ import android.webkit.WebView
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat
@ -247,6 +248,14 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
decor = window.decorView as FrameLayout
webView = binding.webview
val onBackPressed = object : OnBackPressedCallback(webView.canGoBack()) {
override fun handleOnBackPressed() {
if (webView.canGoBack()) webView.goBack()
}
}
onBackPressedDispatcher.addCallback(this, onBackPressed)
webView.apply {
// TODO This quick bar workaround only works on Home Assistant core versions <2022.7
// If not 'fixed' or officially supported: should be removed in Android 2023.1 (GitHub: #2690)
@ -406,6 +415,15 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
}
return false
}
override fun doUpdateVisitedHistory(
view: WebView?,
url: String?,
isReload: Boolean
) {
super.doUpdateVisitedHistory(view, url, isReload)
onBackPressed.isEnabled = canGoBack()
}
}
setDownloadListener { url, _, contentDisposition, mimetype, _ ->
@ -1009,14 +1027,6 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
finish()
}
override fun onBackPressed() {
if (webView.canGoBack()) {
webView.goBack()
} else {
super.onBackPressed()
}
}
override fun loadUrl(url: String) {
loadedUrl = url
webView.loadUrl(url)