0

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

我正在使用以下方法恢复 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);
}

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

4

1 回答 1

1

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

它现在不会重新加载页面,只是重新呈现标题并包含在内,网络历史记录也完好无损。

建议在这里

我修改它以使用片段,这里的片段:

@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());
}
于 2013-07-17T17:52:34.080 回答