Android Question [Solved] Webview and previous URL using KeyPress

asales

Expert
Licensed User
Longtime User
Is possible to use the back key and go to the previous URL?
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
    'How to back to the previous URL ??
    End If
End Sub

Thanks in advance for any tip.
 

JohnC

Expert
Licensed User
Longtime User
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean

    If KeyCode = KeyCodes.KEYCODE_BACK  Then    'back key pressed
        wv.Back        'webview.back
        Return True   'Return True to consume the event
    End If

End Sub

You could also add some code so before doing the wv.back, it will first check to see if the last navigated URL for webview is the first URL that webview navigated to, and if so that means webview is back at it's original starting point/url (meaning the user navigated back to the first page), so another wv.back would do nothing, so when this condition happens, don't consume the back-key and let android handle the back key.
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
try to consume the keypress and inject window.history.back(); via webviewextras into your webview
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
try to consume the keypress and inject window.history.back(); via webviewextras into your webview
I didn't know that window.history.back works differently then webview.back. I thought they worked the same.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
So then the code will look like:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean

    If KeyCode = KeyCodes.KEYCODE_BACK and CanGoBack Then    'back key pressed
        wv.Back        'webview.back
        Return True   'Return True to consume the event
    End If

End Sub
 
Upvote 0
Top