Search code examples
javascriptandroidvariableswebviewsavestate

Does WebView saveState() preserve Javascript variables/environment?


I've searched many threads and still cannot find the answer to my question. I'm working on an Android app which uses WebView.

I use onSaveInstanceState() and onRestoreInstanceState() to save the WebView state like this:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    webView.saveState(savedInstanceState);
}

and

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    webView.restoreState(savedInstanceState);
}

I also have this in my onCreate():

public void onCreate(Bundle savedInstanceState) {
    ... other code ...
    if(savedInstanceState != null){
        webView.saveState(savedInstanceState);
    }else{
        webView.loadUrl("http://mypage");
    }
}

The problem: It seems that restoring WebView does not restore the Javascript variables/environment/workspace at all. When app is killed in the background and then restored, the whole Javascript is gone with all the objects/variables. The javascript code is name-spaced, i.e. window.utilitiesPack, window.eventHandlers, window.automation, etc and they are undefined. Also JQuery and other javascript plugins are used: all of them seem to be undefined after restore state. Basically the whole Javascript is not restored.

Can someone confirm or disprove that this is how it really is (Javascript not saved)? If the whole Javascript workspace is not saved, then what exactly does WebView.saveState() save? Is there some easy/elegant way to use existing API to preserve the Javascript objects?

//======================================================

Update1: So the problem remains. The most significant difficulty is this:

I am launching a Camera Intent for a result. When picture is taken, app gets back to WebView activity and is supposed to use Javascript to update HTML5 LocalStorage with some data variables.

The main Activity with WebView gets killed while Camera Activity is displayed, so when we come back to WebView, there is no Javascript anymore and no functions I can call from Android code. This happens every single time on Galaxy S3. It still happens on other phones, but not every time on picture taking.

Don't know what to do here. Somehow I must make the main Activity with WebView to retain the state while the picture is being taken with the Camera Intent. Does anyone have an idea how this can be achieved?


Solution

  • Very few information found, only this line:

    Please note that this method no longer stores the display data for this WebView. The previous behavior could potentially leak files if restoreState(Bundle) was never called.

    and this line:

    Returns the same copy of the back/forward list used to save the state.

    in the javadoc for WebView.saveState(Bundle).