Skip to content

Commit 5d3f84c

Browse files
feat: enable predictive back navigation for Android 13+ (API 33+)
References: https://outsystemsrd.atlassian.net/browse/RMET-4335
1 parent 79d87ba commit 5d3f84c

2 files changed

Lines changed: 37 additions & 12 deletions

File tree

src/main/AndroidManifest.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
34
package="com.outsystems.plugins.inappbrowser.osinappbrowserlib">
45

56
<uses-permission android:name="android.permission.INTERNET" />
@@ -18,7 +19,9 @@
1819
android:exported="false"
1920
android:configChanges="orientation|screenSize|uiMode"
2021
android:label="OSIABWebViewActivity"
21-
android:theme="@style/AppTheme.WebView" />
22+
android:theme="@style/AppTheme.WebView"
23+
android:enableOnBackInvokedCallback="true"
24+
tools:targetApi="33" />
2225
<activity android:name=".views.OSIABCustomTabsControllerActivity"
2326
android:exported="false"
2427
android:theme="@style/Theme.Transparent"

src/main/java/com.outsystems.plugins.inappbrowser/osinappbrowserlib/views/OSIABWebViewActivity.kt

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class OSIABWebViewActivity : AppCompatActivity() {
8585
filePathCallback = null
8686
}
8787

88+
// for back navigation
89+
private lateinit var onBackPressedCallback: OnBackPressedCallback
90+
8891
companion object {
8992
const val WEB_VIEW_URL_EXTRA = "WEB_VIEW_URL_EXTRA"
9093
const val WEB_VIEW_OPTIONS_EXTRA = "WEB_VIEW_OPTIONS_EXTRA"
@@ -104,19 +107,20 @@ class OSIABWebViewActivity : AppCompatActivity() {
104107
override fun onCreate(savedInstanceState: Bundle?) {
105108
super.onCreate(savedInstanceState)
106109

107-
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
110+
onBackPressedCallback = object : OnBackPressedCallback(true) {
108111
override fun handleOnBackPressed() {
109-
if (options.hardwareBack && webView.canGoBack()) {
112+
if (!webView.canGoBack()) return
113+
if (options.hardwareBack) {
110114
hideErrorScreen()
111115
webView.goBack()
112116
} else {
113-
sendWebViewEvent(OSIABEvents.BrowserFinished(browserId))
114-
webView.destroy()
115-
this.isEnabled = false // disable the callback to prevent it from being called again
116-
onBackPressedDispatcher.onBackPressed()
117+
// if hardwareBack is false, we want to finish the activity (close the WebView)
118+
// and not go back in the WebView history
119+
finish()
117120
}
118121
}
119-
})
122+
}
123+
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
120124

121125
browserId = intent.getStringExtra(OSIABEvents.EXTRA_BROWSER_ID) ?: ""
122126

@@ -152,8 +156,6 @@ class OSIABWebViewActivity : AppCompatActivity() {
152156
closeButton = findViewById(R.id.close_button)
153157
closeButton.text = options.closeButtonText.ifBlank { "Close" }
154158
closeButton.setOnClickListener {
155-
sendWebViewEvent(OSIABEvents.BrowserFinished(browserId))
156-
webView.destroy()
157159
finish()
158160
}
159161

@@ -191,6 +193,18 @@ class OSIABWebViewActivity : AppCompatActivity() {
191193
}
192194
}
193195

196+
override fun onStop() {
197+
super.onStop()
198+
if (isFinishing) {
199+
sendWebViewEvent(OSIABEvents.BrowserFinished(browserId))
200+
}
201+
}
202+
203+
override fun onDestroy() {
204+
webView.destroy()
205+
super.onDestroy()
206+
}
207+
194208
override fun onResume() {
195209
super.onResume()
196210
if (options.pauseMedia) {
@@ -252,7 +266,7 @@ class OSIABWebViewActivity : AppCompatActivity() {
252266
*/
253267
private fun customWebViewClient(
254268
hasNavigationButtons: Boolean,
255-
showURL: Boolean,
269+
showURL: Boolean
256270
): WebViewClient {
257271
return OSIABWebViewClient(hasNavigationButtons, showURL)
258272
}
@@ -301,7 +315,7 @@ class OSIABWebViewActivity : AppCompatActivity() {
301315
*/
302316
private inner class OSIABWebViewClient(
303317
val hasNavigationButtons: Boolean,
304-
val showURL: Boolean
318+
val showURL: Boolean,
305319
) : WebViewClient() {
306320

307321
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
@@ -386,6 +400,14 @@ class OSIABWebViewActivity : AppCompatActivity() {
386400
}
387401
}
388402

403+
override fun doUpdateVisitedHistory(view: WebView?, url: String?, isReload: Boolean) {
404+
// to implement predictive back navigation
405+
// we only want to have the callback enabled if the WebView can go back to previous page
406+
// if not, we want the system to handle the back press, which will enable the
407+
// predictive back animation
408+
onBackPressedCallback.isEnabled = webView.canGoBack()
409+
}
410+
389411
/**
390412
* Responsible for handling and launching intents based on a URL.
391413
* @param intentAction Action for the intent

0 commit comments

Comments
 (0)