我使用此解决方案修复了它: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());
}