Does WebView saveState () save Javascript / environment variables?

I searched a lot of threads and still can not find the answer to my question. I am working on an Android application that uses WebView .

I use onSaveInstanceState() and onRestoreInstanceState() to save the state of the WebView as follows:

 @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"); } } 

Problem: Restoring WebView does not seem to restore Javascript variables / environment / workspace at all. When the application is killed in the background and then restored, all Javascript is gone with all objects / variables. The javascript code is exploded by name, i.e. window.utilitiesPack , window.eventHandlers , window.automation , etc., and they are undefined. JQuery and other javascript plugins are also used: they all look undefined after a recovery state. Basically, all Javascript is not restored.

Can someone confirm or deny that it is so (Javascript is not saved)? If the entire Javascript workspace is not saved, then what exactly does WebView.saveState() save? Is there a simple / elegant way to use an existing API to save Javascript objects?

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

Update1: So the problem remains. The most significant problem is the following:

I run Camera Intent for the result. When the snapshot is taken, the application returns to WebView activity and is supposed to use Javascript to update HTML5 LocalStorage with some data variables.

The main action with WebView will be killed when the camera activity is displayed, so when we return to WebView, there will be no more Javascript and there are no functions that I can call from Android code. This happens every time on the Galaxy S3 . This still happens on other phones, but not every time when shooting.

I don’t know what to do here. Somehow I have to do the main Activity with a WebView in order to maintain a state when the image is executed using the camera’s intent. Does anyone know how this can be achieved?

+6
source share
2 answers

Very little information was found, only this line:

Note that this method no longer stores 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 list back / forward, which is used to save state.

in javadoc for WebView.saveState(Bundle) .

+3
source

As user2113581 comments, moving a WebView in the context of the application, not in the Activity, is a potential solution. This still has flaws that user 21113581 mentioned, including <select> not working (because it creates a window, but there is no window token in the application context).

I expanded 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 mWebView to the container view (FrameLayout or the like) during the life cycle event, which makes sense. You will need to remove it from the container when the action is paused or stopped. I used onResume and onPause with good results.

+6
source

All Articles