WebView web page keeps running

twodogs

Member
Licensed User
Longtime User
I have a webview that loads a url on activity create. The webpage has a javascript timer in it that runs a XMLHttpRequest at set intervals to update the html.

When the activity is paused, that javascript keeps going. I know as I can see the requests hitting the web server. It will do so as long as the activity process exists.

Then, on resuming the activity, the url is loaded back on the WebView, and now there are 2 webpages and javascript timers running! Pause and resume again, 3 timers etc.

Why does the webpage javascript keep running when the activity is paused, can it be stopped and how to make the activity's webview not start another page?

Thanks,

BTW First real project with B4A, great product.
 

warwound

Expert
Licensed User
Longtime User
Do you have any control over the page content?

If so then you could add a javascript function that stops the timer.
Then in your B4A Activity you can call that new javascript function when the Activity is paused.

B4X:
Sub Activity_Pause (UserClosed As Boolean)
   MyWebView.LoadUrl(****)
End Sub

Replace **** with "javascript:myNewFunctionName()", the forum software won't let me post "javascript:myNewFunctionName()" inside of the code block!

Or you could use my WebViewExtras library to execute the same javascript using it's executeJavascript method - the result will be the same.

If you have no control over the page content then try loading an empty HTML page when your Activity is paused:

B4X:
Sub Activity_Pause (UserClosed As Boolean)
   MyWebView.LoadHtml("<!DOCTYPE html><html><head><title></title></head><body></body></html>")
End Sub

When your device starts to run low on memory then Android will run it's garbage collector and destroy unused objects to reclaim memory - until that happens any objects created (such as your WebView) will continue to exist.

Martin.
 
Upvote 0

twodogs

Member
Licensed User
Longtime User
Hi Martin,

I like the suggestion of the blank page, otherwise I'm doing all this now including setting javascript enabled to false on pause, and then to true on resume plus re-starting the javascript timer. Seems a dirty fix.


Couldn't do any of this or what I need at all without WebViewExtras. :sign0098:

Thanks,
Bruce
 
Last edited:
Upvote 0
Top