Android Question Wait For and WebView PageFinished

iglrs1

Member
Licensed User
Longtime User
Hi,

I want to wait for a webpage to finished but I dont want that the program continues.

When a use the Wait For comand the program return an continue. Is it posible to wait for the page to be finished?
Thanks

B4X:
Sub LoadWebPage(Address As String)
    
    WebView1.LoadUrl(Address)
    Wait For WebView1_PageFinished(Url As String)

    ...
End sub
 

William Lancee

Well-Known Member
Licensed User
Longtime User
B4X:
'Put this in your calling program instead of LoadWebPage(Address)
Wait for (LoadWebPage(Address)) Complete (Success As Boolean)
   
Sub LoadWebPage(Address As String) As ResumableSub
    WebView1.LoadUrl(Address)
    Wait For WebView1_PageFinished(Url As String)
    ...
    Return True
End sub
 
Upvote 0

iglrs1

Member
Licensed User
Longtime User
Thanks. The fact is that LoadWebPage is a function inside a class(Web) a if I write it as

B4X:
'Class Web

Sub Class_Globals
End Sub

Sub LoadPage(address)
   Wait for (LoadWebPage(Address)) Complete (Success As Boolean)
End sub

Sub LoadWebPage(Address As String) As ResumableSub
    WebView1.LoadUrl(Address)
    Wait For WebView1_PageFinished(Url As String)
    ...
    Return True
End sub

a tried to call like that

B4X:
Dim www as Web
www.LoadPage("www.google.com")

the code continue and doesn't wait to load the web page
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Yes that makes a difference. You need to have a callback function instead.

Note that I haven't tested this.

B4X:
Sub Class_Globals
    Private Callback As Object
End Sub

Public Sub Initialize (CallbackModule As Object)
    Callback = CallbackModule
End Sub

Sub LoadWebPage(Address As String) As ResumableSub
    WebView1.LoadUrl(Address)
    Wait For WebView1_PageFinished(Url As String)
    ...
    CallSub(Callback, "LoadWebPage_Completed")
End sub

B4X:
Dim www as Web
www.Initialize(Me)
www.LoadPage("www.google.com")
Wait For LoadWebPage_Completed
 
Upvote 0

Walter Brunmueller

Member
Licensed User
Longtime User
Hello!

Please post the complete class-code, because I get some errors
and I am not so familiar with this type of programming

- WebView1 is not declaired
- www.LoadPage is the class method but LoadWebPage is declaired

Thank you for your help!
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Sure. I chose the B4XPages template as a working example.

In B4XMainPage:

B4X:
Private Sub B4XPage_Appear
    Log("LOAD REQUESTED1")
    Dim www As web
    www.Initialize(Me)
    www.LoadWebPage("www.google.com")
    Wait For LoadWebPage_Completed
    Log("LOADED")
End Sub


The Class (webview1 is declared in code, but you could use the designer and load the layout file):

Ed: I changed my mind about the parameters in initialize (since in this case root is not needed).

B4X:
Sub Class_Globals
    Private Callback As Object
    Private webview1 As WebView
End Sub

Public Sub Initialize (CallbackModule As Object)
    Callback = CallbackModule
    webview1.Initialize("WebView1")
End Sub

Public Sub LoadWebPage(Address As String)
    Log("LOAD REQUESTED2")
    webview1.LoadUrl(Address)
    Wait For WebView1_PageFinished(Url As String)
    Log("PAGE FINISHED")
    '...
    CallSub(Callback, "LoadWebPage_Completed")
End Sub
 
Last edited:
Upvote 0
Top