Webview方向更改webview丢失标头

时间:2013-07-17 17:14:44

标签: android android-webview

当我旋转屏幕时,网页重新加载,但没有原始标题。

我正在使用以下方法恢复webview状态:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState == null) {
        // Restore the state of the WebView
        // webView.getRefreshableView().restoreState(savedInstanceState);
        webView.loadUrl("my_url");
    } else {
        webView.restoreState(savedInstanceState);
    }
}

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

关于解决方案的任何想法?我真的想恢复状态,因为它包含所有的webhistory,但我需要重新加载页面时的标题!

1 个答案:

答案 0 :(得分:1)

我使用此解决方案修复了它: http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/

它现在重新加载页面,只是重新渲染所以标题和包含,网络历史记录也完好无损。

建议here

我将其修改为使用片段,片段:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.web_fragment, null);
    //webContainer = () view.findViewById(R.id.web_container);
    initUI(view);
    setupWebView();
    return view;
}

protected void initUI(View view)
{
    // Retrieve UI elements
    webViewPlaceholder = ((FrameLayout)view.findViewById(R.id.webViewPlaceholder));
     // Initialize the WebView if necessary
    if (webView == null)
    {
        // Create the webview
        webView = new JSWebView(getActivity());
        webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

        // Load the URLs inside the WebView, not in the external web browser
        webView.setWebViewClient(new WebViewClient());

        // Load a page
        webView.loadUrl("http://someinitialurl.com");
    }

    // Attach the WebView to its placeholder
    webViewPlaceholder.addView(webView);
}

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    if (webView != null)
    {
        // Remove the WebView from the old placeholder
        webViewPlaceholder.removeView(webView);
    }

    super.onConfigurationChanged(newConfig);

    // Reinitialize the UI
    initUI(getView());
}