开发者

Android: How to saveState of a WebView with an addJavascriptInterface attached?

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());
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜