publicknowledge publicknowledge - 5 months ago 37
Android Question

Android webview unresponsive after long sleep

I have a little hobby app I'm working on. It utilizes a webview in the main layout. Clicking on links inside the webview keeps the user within the webview. Everything is fine after starting it up and while still inside the app. However, after the phone sleeps for some time and I reopen the app to surf from where I left off, I notice some unexpected behavior.

  1. the webpage loads texts but not images (question marks or just blank space where the images are expected to be).

  2. clicking on links inside the webpage inside the webview takes the user out of the webview and tries to open an external browser.

As a reference, I have already done:

protected void onSaveInstanceState(Bundle outState) {

protected void onRestoreInstanceState(Bundle savedInstanceState) {

as well as this inside the onCreate code:

if (savedInstanceState != null) {
// this means my app is starting from scratch and do the necessary webview loading

I'm not sure what I'm missing but webview seems, erm, "broken", for lack of better term, after a long sleep. I've tried searching for more information regarding this but don't see more people encountering this issue. Either people don't expect webview to be used in this manner OR I'm doing something wrong. I think it's the latter.

Thanks for any feedback.

More Info:
The webview is the root activity of the app. Steps to replicate issue is just pressing main menu while in webview so the app pauses in webview activity, wait or let phone sleep, turn on phone, open app and the activity restarts on the last page webview was loading. Wait long enough and the webview activity will exhibit the behaviors mentioned.

I have also setup basic life cycle functions onResume, onPause, onStop, onDestroy all calling their respective super functions etc..

I also do use WebViewClient and WebChromeClient.

My WebViewClient solely takes care of "onPageStarted", "onReceivedError", "shouldOverloadUrlLoading" and "onPageFinished".

My WebChromeClient on the other hand, only does "onGeolocationPermissionShowPrompt" and "onProgressChanged".

Both work to create a loading bar on top of the webview to show page load progress.


So, I've been doing quite a few tests but most useful was the Toast messages in each of the lifecycle onResume, onPause, onStop, onDestroy as well as onSaveInstanceState and onRestoreInstanceState.

I was using a block of code like so (to restore state) [rewrite of the above code]:

    // do the webview getsettings such as setUseWideViewPort and setSupportZoom
    // setInitialScale, setJavaScriptEnabled, etc..
    // as well as setting WebViewClient and WebChromeClient
    // then preloading the webview with the default url

Due to the Toast messages inside for debugging, I noticed that after a long sleep, Android apparently kills my activity and the onCreate is called again. The thing I did not account for was webview.restoreState not restoring everything about the webview.

It does not restore anything inside the "else" block above. In effect, anything that was setup during that time disappears. Therefore, only the website was saved and none of the WebView and WebChrome client were.

This caused the behavior mentioned above.

By moving setup block from the "else" block outside of it, my webview was able to set itself up again with the necessary settings.

Added Info: I also removed the onRestoreInstanceState() protected function as it does not seem to be doing anything where I am concerned.

Comments and suggestions are still welcome in case I overlooked anything in the solution. Thanks.