Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 370
user2113581 Avatar asked Feb 27 '13 02:02

user2113581


2 Answers

As user2113581 commented, moving the WebView into a context of the application rather than the Activity is a potential solution. This still has all of the pitfalls that user2113581 mentioned, including <select> not working (because it creates a window, but an application context does not have a window token).

I have extended my Application to manage my webview...

MyApplication.java:

public class MyApplication extends Application {
  private WebView mWebView;
  private boolean mWebViewInitialized;
  // ...

  @Override public void onCreate() {
    super.onCreate();
    mWebView = new WebView(this);
    mWebViewInitialized = false; // we do some initialization once in our activity.
  }

  public WebView getWebView() { return mWebView; }
  public boolean isWebViewInitialized() { return mWebViewInitialized; }
  public void setWebViewInitialized(boolean initialized) {
    mWebViewInitialized = initialized;
  }
}

In our activity:

@Override protected void onCreate(Bundle savedInstanceState) {
  // ...
  MyApplication app = (MyApplication) getApplication();
  mWebView = app.getWebView();
  if (!app.isWebViewInitialized()) { 
    /* first time initialization */ 
    app.setWebViewInitialized(true);
  }
}

Finally, in your Activity, you will want to add the mWebView to a container view (FrameLayout, or similar) during a lifecycle event that makes sense. You'll need to remove it from the container when the activity is being paused or stopped. I've used onResume and onPause with good results.

like image 137
Ryan Schultz Avatar answered Sep 21 '22 14:09

Ryan Schultz


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).

like image 38
diyism Avatar answered Sep 22 '22 14:09

diyism