I have an android WebView that has a JavaScript interface added with addJavajavascriptInterface().
Is it possible to save the state of the WebView (webpage state, html, css, javascript) and then restore it on onCreate? If yes, how?
The code I have so far keeps crashing:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
final WebBackForwardList list = mWebView.restoreState(savedInstanceState);
if (savedInstanceState.containsKey("currentPicture")) {
final File f = new File(savedInstanceState.getString("currentPicture"));
mWebView.restorePicture(savedInstanceState, f);
f.delete();
}
} else {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "AndroidApp");
mWebView开发者_如何学Go.loadUrl(url);
}
}
public void onSaveInstanceState(Bundle outState) {
final WebBackForwardList list = mWebView.saveState(outState);
File mThumbnailDir = getDir("my-thumbnails", 0);
if (list != null) {
final File f = new File(mThumbnailDir, mWebView.hashCode() + "_pic.save");
if (mWebView.savePicture(outState, f))
outState.putString("currentPicture", f.getPath());
}
}
精彩评论