How to save the state of a WebView inside a Fragment of an Action Bar

The idea for this post came to me after reading the comment of Esthon Wood: “do you have any idea on how to save the state of the WebView? I mean, the webview refreshes everytime I tap on the tab.” in the post Tab Layout in Android with ActionBar and Fragment.

As a starting point I suppose that you have properly configured the example explained in Tab Layout in Android with ActionBar and Fragment.

In the following steps I configure a WebView in the second tab:

  1. replace the file res/layout/tab2.xml
  2. replace the eu/lucazanini/Tab2Fragment.java

    • row 24: I set the variable webView to the WebView in the layout res/layout/tab2.xml
    • row 25: with this instruction the WebView is displayed under the second tab, and not in full screen hiding the Action Bar, and every link is opened reusing the same webView without opening the default browser; the issue is that I no longer have the address bar
    • row 38: method onPause, here I save the state of the webView in the Bundle webViewBundle; the method onPause is called when the Fragment is going into the background (for example when I select the other tab), after the reselection of the second tab, in the onCreate method after verifying that there is a webViewBundle not null, I use it to restore the state of the WebView (row 30)
  3. add a permission to access to internet in AndroidManifest.xml
  4. launch the app
    webview

For other configurations of the WebView, such as to enable JavaScript, you can refer to Building Web Apps in WebView.

11 Comments

 Add your comment
  1. how to create method onKeyDown in this Fragment?

  2. Hi,

    I’m a bit new to android. I nliked your example and it work really Good – Thank you.

    Could you please advise how do I add a progress bar to it?

  3. Post is excellent, i am looking for it since long time. as Mr.Adam Croth said. I got Same problem. Javascript is not working.

    Any solution for it.

    Thanks

  4. I’m using a fixed tab with swipe. There’s only one fragment they all share, and one webView that is always instantiated in my onCreateView. Pls how can I save the webView state for 6 TABS. it only saves for the first two. thanks

  5. Thank you for your tutorial,

    I have a problem, even I inserted Javascript activation code to one Fragment, Javascript is not working.

    String url = “http://www.site.com/”;

    webView = (WebView) ll.findViewById(R.id.webView3);
    webView.setWebViewClient(new WebViewClient());

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptEnabled(true);

    if (webViewBundle == null) {
    webView.loadUrl(url);
    } else {
    webView.restoreState(webViewBundle);
    }
    return ll;
    }

    How can I solve this problem?

    Thanks

  6. Friend, I am using your article as reference when I return in the webview Tab 2 takes about two seconds to reload, there is a way to solve this case? Thanks!!

    • You can try to replace the onTabSelected and onTabUnselected methods of the inner class TabListener inside TabActionBarActivity.java

      where I use “show” and “hide” instead of “attach” and “detach” at the lines 10 and 18.

  7. i used your idea. but when we rotate the phone or orientation changes , the links start opening in a new browser.

    how can we prevent that ??

    • I don’t have the issue you said.
      The row 25 in Tab2Fragment.java should prevent that.

      I think you don’t need but you can explicitly handle the page navigation as explained here.
      The shouldOverrideUrlLoading method should be:

      Go here for more info.

Leave a Comment

Your email address will not be published.