Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 恢复WebView的状态_Android_Webview - Fatal编程技术网

Android 恢复WebView的状态

Android 恢复WebView的状态,android,webview,Android,Webview,我有webview用于显示来自Disqs服务的内容。以下是片段代码的一部分: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_coments, container, false);

我有webview用于显示来自Disqs服务的内容。以下是片段代码的一部分:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_coments, container, false);

        mComentsView = (WebView)root.findViewById(R.id.coments_view);

        String htmlComments = getHtmlComment(createId(), "androidaci");

        mComentsView.getSettings().setJavaScriptEnabled(true);
        mComentsView.setWebViewClient(new ComentsWebViewer(htmlComments));
        mComentsView.setWebChromeClient(new WebChromeClient());
        if(savedInstanceState != null){
            Log.e("restoring", "state");
            mComentsView.restoreState(savedInstanceState);
        } else {
            Log.e("loading", "website");
            mComentsView.loadData(htmlComments, "text/html", null);
        }
        return root;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mComentsView.saveState(outState);
    }


    public String getHtmlComment(String idPost, String shortName) {

        return "<div id='disqus_thread'></div>"
                + "<script type='text/javascript'>"
                + "var disqus_identifier = '"
                + idPost
                + "';"
                + "var disqus_shortname = '"
                + shortName
                + "';"
                + " (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;"
                + "dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';"
                + "(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();"
                + "</script>";
    }
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根=充气机。充气(R.layout.fragment\u coments,container,false);
mComentsView=(WebView)root.findviewbyd(R.id.coments\u视图);
字符串htmlComments=getHtmlComment(createId(),“androidaci”);
mComentsView.getSettings().setJavaScriptEnabled(true);
setWebViewClient(新ComentsWebViewer(htmlComments));
setWebChromeClient(新的WebChromeClient());
如果(savedInstanceState!=null){
Log.e(“恢复”、“状态”);
mComentsView.restoreState(savedInstanceState);
}否则{
Log.e(“加载”、“网站”);
加载数据(htmlComments,“text/html”,null);
}
返回根;
}
@凌驾
SaveInstanceState上的公共无效(束超出状态){
super.onSaveInstanceState(超出状态);
mComentsView.saveState(超出状态);
}
公共字符串getHtmlComment(字符串idPost、字符串shortName){
返回“”
+ ""
+“var discus_标识符='”
+idPost
+ "';"
+“var discus_shortname=”
+短名
+ "';"
+“(function(){var dsq=document.createElement('script');dsq.type='text/javascript';dsq.async=true;”
+“dsq.src='http://'+discus_shortname+'.discus.com/embed.js'
+“(document.getElementsByTagName('head')[0]| | document.getElementsByTagName('body')[0])。appendChild(dsq);})()”
+ "";
}
问题在于webview重新加载时的轮换,我想知道原因。我试图保存和恢复状态,在日志中我得到了恢复状态语句,但它看起来仍然在重新加载旋转更改。如何防止重新加载

使用此代码

  @Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the WebView state (including history stack)
    mWebView.saveState(savedInstanceState);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

到您在AndroidManifest.xml上的活动

android:configChanges="keyboard|keyboardHidden|screenSize|orientation" 
和您的Java代码

// survive screen orientation change
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

对我有效:)

不,结果还是一样