问题
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?
回答1:
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).
回答2:
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.
来源:https://stackoverflow.com/questions/15103141/does-webview-savestate-preserve-javascript-variables-environment