Android Question Back button in a Webview

Mark Read

Well-Known Member
Licensed User
Longtime User
I think I have shot myself in the knee with this one. If I have a webview, load an URL then click on a link, how do I get back to the page beforehand?

Unfortunately I have trapped the back button. If the back button is pressed and the webview is visible, I hide it and pass "true" to kill the event.

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean

    If KeyCode = KeyCodes.KEYCODE_BACK Then

       If Webview1.Visible=True Then Webview1.Visible=False
       Return True
    End If
End Sub

Searching the forum has not helped. Am I looking at the wrong sub???
Many thanks.
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Aha, I didn't know about Webview.Back, that takes you back one level in the browser history?

The Webview is placed over the main activity and is only used to check. The back button hides it again.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
If I understand you Erel, this should work (can't test at present). In the webview I only want to go back one page.

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If BackPressed=false then    'Previously dimmed as false
            BackPressed=true
            WebView1.Back
            Return True
        Else
            BackPressed=False
            If Webview1.Visible=True Then Webview1.Visible=False
            Return True
        End If
    End If
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
If I understand you Erel, this should work (can't test at present). In the webview I only want to go back one page.

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If BackPressed=false then    'Previously dimmed as false
            BackPressed=true
            WebView1.Back
            Return True
        Else
            BackPressed=False
            If Webview1.Visible=True Then Webview1.Visible=False
            Return True
        End If
    End If
End Sub
This will work but I think that the user will need to click the back button twice to exit the webView when she didn't click any link.

By the way what is the user supposed to check using a webView? Just in case there may be an alternative way to do it.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Look at this page: http://b4a.martinpearman.co.uk/webviewextras/WebViewExtras2.html
Search the page for 'CanGoBack'.
You'll see a WebView has a CanGoBack method that returns a Boolean value.

This method isn't exposed in the default b4a WebView.
You might want to use CanGoBack to decide whether you want to go back or hide the WebView.

If you don't want to use WebViewExtras2 then you could instead use JavaObject to access the native canGoBack method.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
This will work but I think that the user will need to click the back button twice to exit the webView when she didn't click any link.

Oops, that is correct. The user is only me but I could still live with having to press twice.

I will look into Warwounds option as well.

Many thanks.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
I am posting this snippet more to let u know that you can test if it even possible to go back a page .
I am unsure whether it is of any use to you ...
B4X:
If CanGoBack(WebView1) Then  'test if webview can go back another page

Sub CanGoBack(wv As WebView) As Boolean
   Dim r As Reflector
   r.Target = wv
   Return r.RunMethod("canGoBack")
End Sub
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Either you use the code in post #4 which uses the back button or you create your own button and on the click event use
WebView1.Back.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Layout1 has a button and a webview. Reflection library is required.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Private WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub WebView1_PageFinished (Url As String)
   
End Sub
Sub WebView1_OverrideUrl (Url As String) As Boolean
   
End Sub
Sub Button1_Click
    If CanGoBack(WebView1) Then  WebView1.Back         'test if webview can go back another page   
End Sub



Sub CanGoBack(wv As WebView) As Boolean
   Dim r As Reflector
   r.Target = wv
   Return r.RunMethod("canGoBack")
End Sub
 
Upvote 0

Baris Karadeniz

Active Member
Licensed User
Thank you. It is ok but there are 3 things.

1) To write username and password to log in to the system at the first webview screen I need to zoom in. When I zoom and log in, second view comes to screen. If I press on a back button, I go back to first view but to the zoom view. How can I go back to all view?

2) If I press on a button at the second view, I go to third view. There are anchor links at the third view. When I press on the anchor link I go to the middle of the page for example. In this case if I press on the back button it goes to the previous view at the same screen not to previous screen.

3) When I press on the back button at the third view, it goes to the second view but it reloads the page. How can I prevent reloading?

Thanks in advance.
 
Upvote 0
Top