Android:如何使用附加的addJavascriptInterface保存WebView的状态?

时间:2011-01-18 16:46:25

标签: javascript android webview savestate

我有一个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());
    }
}

0 个答案:

没有答案