Android Question WebView sometimes freezes

canalrun

Well-Known Member
Licensed User
Longtime User
I have an app I've been using for more than five years. It's basically a database of comics site's web addresses that I can scroll through and display in a web view. In addition to the comic each page also displays a ton of ads from the comic's website. I've used this app on 4 or 5 different tablets, from different manufacturers, using different versions of Android (currently Android 11 on a Samsung).

Sometimes I will make it through the entire database with no pages locking up.

Randomly when a page is being displayed, the WebView will lock up – the page doesn't finish loading, and I can no longer load other pages by tapping Prev or Next.

When the web view locks up, if I exit the app, using the back button, then restart the app, the web view will remain locked up. If I wait a long amount of time before restarting the app (sometimes 15 minutes, sometimes more than an hour), it will work again. I attribute this to Android finally killing my app when it feels like it after period of time.

If I exit the app, using the back button, go into settings and Force Stop the app, then reopen the app, the web view will display pages properly.

My best guess at what's happening is that pages sometimes try to display an ad or an ad from a source that causes the web view to go into an infinite loop. Killing the app also kills that instance of the web view and the infinite loop is killed.

Doing a Google search finds many people with problems of WebViews freezing, but no solutions and few details.

Is there a way to reset the WebView? I've tried StopLoading, but that doesn't seem to work. I can also see resetting the WebView might interfere with the way B4A initializes it.

Can I force a ForceStop when I close the app? There is really no reason to keep the app running in the background and if it locks up, a Force Stop will save me from having to navigate through settings.

Any other suggestions?

Thanks,
Barry.
 

JohnC

Expert
Licensed User
Longtime User
Have you already tried adding the chromeclient to the webview as mentioned in tip #1 of my below signature?
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks.
I added these three lines:
B4X:
Dim wv as WebView
Dim wve as WebViewExtras 
wve.addWebChromeClient(wv, "WVE")
Unfortunately it still freezes once in a while (although it worked properly again after a Force Stop, then reopen).

To be sure I added those lines correctly…
I added the WebViewExtras library and added those three lines to my code. I still use my WebView to load the URL.

Any idea of a way to Force Stop the app upon exit?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I still use my WebView to load the URL.
When you say "your webview", it kind of makes me think that it might be different then the "wv" in those three lines.

So I just wanted to make sure that you renamed "wv" in those three lines to the name of your webview right?

I ask because you need to add the chrome client to "your" webview for it to have any chance of possibly fixing your issue.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Yup. My actual lines of code (… represents unrelated removed code)

B4X:
Sub Globals
 ...
  Private wvMain As WebView
  Private wvExt As WebViewExtras
 ...
End Sub

Sub Activity_Create(FirstTime As Boolean)
 ...
  wvExt.addWebChromeClient(wvMain, "wvExt")
 ...
End Sub

Sub StepComicList(dir As Int) As Boolean
 ...
   wvMain.LoadUrl(s)
 ...
End Sub
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Well, it looks correct, so I guess the chrome client didn't work.

This command is not supposed to be used, but have you tried using the command "ExitApplication" to force close the app when you end it?
 
Last edited:
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks for your help. I think I tried ExitApplication years ago with no luck. I will give it a try again.

Does ExitApplication do a force stop? I was under the impression that it merely did the same thing as a press of the Android Back button or Home button.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I think it does do a force close, so that's why I think its worth a try.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Yes! I just found this in the documentation.

Method_636.png
ExitApplication

Immediately ends the application and stops the process.
Most applications should not use this method and prefer Activity.Finish which lets the OS decide when the process is killed.

Maybe I was confusing it with Activity.Finish.

Thanks. Definitely will try.

… Added Later…
Going bald time :confused:

Of course it's not that simple. I gave using ExitApplication a quick try. It didn't seem to work.

Reading B4A forum posts again I see ExitApplication can cause a restart of the app if there are open activities or services. Using WebView I will bet there are open services I don't know about.

Programmatically forcing a Force Stop seems more complicated.
 
Last edited:
Upvote 0
Top