Android Question How to update WebView from class

redbeardrob

Member
Licensed User
Longtime User
I have a class that needs to trigger a webview to refresh, when it recieves input from a service that monitors incoming sms.
In c++ I would pass a pointer to the webview into the class's sub. How do I do that in b4a? I have searched forums and don't see anything, but I might be using the wrong vocabulary.
The class can't see "Webview1" as it is part of the main Activity and doesn't exist to the compliler yet.
If I can't update the activity's webview from a class, and instead need to declare a webview inside the class, do I just set it's parent and attributes in Activity load to make it show up?
This class so far doesn't have any UI components. It triggers from a service that monitors incoming sms. I need it to be able to run in the background and not be put to sleep, and I read somewhere on the forum here that is best to keep ui elements out of classes and background services.
Thank you so much! Sorry if this isn't formatted the best I've been searching the forums and trying to get back into b4a.
If all else fails I guess I can have a timer in the Activity check for a flag in the class but that's so ugly and wasteful.
 

drgottjr

Expert
Licensed User
Longtime User
your class can talk to the main element using the "callsub" methods. for example, in your class you could have something like:

Some code in class:
Sub Class_Globals
  
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    CallSubDelayed2(Main, "loadMe", "https://www.b4x.com")
End Sub

this simple example runs the loadMe method in the activity. in this case, it passes the url that it wants webview to load.
===================================================================================
in the main element, you would have something like:
Example code in Main:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Dim webview As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    webview.Initialize("")
    Activity.AddView(webview,0%x,0%y,100%x,100%y)
    Dim jac As JustAClass
    jac.Initialize
End Sub

Sub loadMe(Url As String)
    webview.LoadUrl( Url )
End Sub

in main you would have a method corresponding to the one called in your class. class causes main to load a particular url. class would call the method at the appropriate moment; for the example i do it on initialization of the class (which doesn't need to have an initialization if you don't have anything particular to carry out in such a method). in your case, you would call callsub2() when an sms was received.

since you mention sms, i should mention that you would have to convince google to allow you to handle sms's if you were thinking of publishing the app on play.
 

Attachments

  • pointer.jpg
    pointer.jpg
    44.4 KB · Views: 161
Last edited:
Upvote 0
Top