@@ -24,6 +24,7 @@ import android.widget.Button
2424import android.widget.ImageButton
2525import android.widget.LinearLayout
2626import android.widget.TextView
27+ import androidx.activity.OnBackPressedCallback
2728import androidx.activity.enableEdgeToEdge
2829import androidx.activity.result.contract.ActivityResultContracts
2930import androidx.appcompat.app.AppCompatActivity
@@ -84,6 +85,9 @@ class OSIABWebViewActivity : AppCompatActivity() {
8485 filePathCallback = null
8586 }
8687
88+ // for back navigation
89+ private lateinit var onBackPressedCallback: OnBackPressedCallback
90+
8791 companion object {
8892 const val WEB_VIEW_URL_EXTRA = " WEB_VIEW_URL_EXTRA"
8993 const val WEB_VIEW_OPTIONS_EXTRA = " WEB_VIEW_OPTIONS_EXTRA"
@@ -103,6 +107,15 @@ class OSIABWebViewActivity : AppCompatActivity() {
103107 override fun onCreate (savedInstanceState : Bundle ? ) {
104108 super .onCreate(savedInstanceState)
105109
110+ onBackPressedCallback = object : OnBackPressedCallback (true ) {
111+ override fun handleOnBackPressed () {
112+ if (! webView.canGoBack()) return
113+ hideErrorScreen()
114+ webView.goBack()
115+ }
116+ }
117+ onBackPressedDispatcher.addCallback(this , onBackPressedCallback)
118+
106119 browserId = intent.getStringExtra(OSIABEvents .EXTRA_BROWSER_ID ) ? : " "
107120
108121 sendWebViewEvent(OSIABWebViewEvent (browserId, this @OSIABWebViewActivity))
@@ -137,8 +150,6 @@ class OSIABWebViewActivity : AppCompatActivity() {
137150 closeButton = findViewById(R .id.close_button)
138151 closeButton.text = options.closeButtonText.ifBlank { " Close" }
139152 closeButton.setOnClickListener {
140- sendWebViewEvent(OSIABEvents .BrowserFinished (browserId))
141- webView.destroy()
142153 finish()
143154 }
144155
@@ -176,6 +187,18 @@ class OSIABWebViewActivity : AppCompatActivity() {
176187 }
177188 }
178189
190+ override fun onStop () {
191+ super .onStop()
192+ if (isFinishing) {
193+ sendWebViewEvent(OSIABEvents .BrowserFinished (browserId))
194+ }
195+ }
196+
197+ override fun onDestroy () {
198+ webView.destroy()
199+ super .onDestroy()
200+ }
201+
179202 override fun onResume () {
180203 super .onResume()
181204 if (options.pauseMedia) {
@@ -237,7 +260,7 @@ class OSIABWebViewActivity : AppCompatActivity() {
237260 */
238261 private fun customWebViewClient (
239262 hasNavigationButtons : Boolean ,
240- showURL : Boolean ,
263+ showURL : Boolean
241264 ): WebViewClient {
242265 return OSIABWebViewClient (hasNavigationButtons, showURL)
243266 }
@@ -249,20 +272,6 @@ class OSIABWebViewActivity : AppCompatActivity() {
249272 return OSIABWebChromeClient ()
250273 }
251274
252- /* *
253- * Handle the back button press
254- */
255- override fun onBackPressed () {
256- if (options.hardwareBack && webView.canGoBack()) {
257- hideErrorScreen()
258- webView.goBack()
259- } else {
260- sendWebViewEvent(OSIABEvents .BrowserFinished (browserId))
261- webView.destroy()
262- onBackPressedDispatcher.onBackPressed()
263- }
264- }
265-
266275 /* *
267276 * Handle permission requests
268277 */
@@ -300,7 +309,7 @@ class OSIABWebViewActivity : AppCompatActivity() {
300309 */
301310 private inner class OSIABWebViewClient (
302311 val hasNavigationButtons : Boolean ,
303- val showURL : Boolean
312+ val showURL : Boolean ,
304313 ) : WebViewClient() {
305314
306315 override fun onPageStarted (view : WebView ? , url : String? , favicon : Bitmap ? ) {
@@ -385,6 +394,15 @@ class OSIABWebViewActivity : AppCompatActivity() {
385394 }
386395 }
387396
397+ override fun doUpdateVisitedHistory (view : WebView ? , url : String? , isReload : Boolean ) {
398+ // to implement predictive back navigation
399+ // we only want to have the callback enabled if the WebView can go back to previous page
400+ // and if the hardwareBack option is enabled
401+ // if not, we want the system to handle the back press, which will enable the
402+ // predictive back animation and simply close the WebView
403+ onBackPressedCallback.isEnabled = webView.canGoBack() && options.hardwareBack
404+ }
405+
388406 /* *
389407 * Responsible for handling and launching intents based on a URL.
390408 * @param intentAction Action for the intent
0 commit comments