0

When I rotate the screen the webpage reloads, but without my original headers.

I am restoring the webview state using:

@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);
}

Any ideas on a solution? I would really like to restore state, as that contains all the webhistory, but I need the headers when the page is reloaded!

serenskye
  • 3,467
  • 5
  • 35
  • 51

1 Answers1

1

I fixed it using this solution: http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/

It now doen'st reload the page, just re-renders so headers and included, web history is also intact.

suggested here

I modified it to work with fragments, snippets 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());
}
Community
  • 1
  • 1
serenskye
  • 3,467
  • 5
  • 35
  • 51