Android:如何保存附加了 addJavascriptInterface 的 WebView 的状态?
我有一个 android WebView,它有一个使用 addJavajavascriptInterface() 添加的 JavaScript 接口。
是否可以保存WebView的状态(网页状态,html,css,javascript),然后在onCreate上恢复它?如果是,怎么办?
到目前为止我的代码一直崩溃:
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.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());
}
}
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.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());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论