Android webView saveState

Ukjent picture Ukjent · Apr 21, 2012 · Viewed 24.5k times · Source

I have a tabhost with 3 tabs. In each of these tabs there is a webiview. When i click a tab the webview need to "reload" even when i have been there before, it have not been saved. Is there any way to save the webview ?

Answer

Shankar Agarwal picture Shankar Agarwal · Apr 21, 2012

This can be handled by overrwriting onSaveInstanceState(Bundle outState) in your activity and calling saveState from the webview:

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

Then recover this in your onCreate after the webview has been re-inflated of course:

@Override
public void onCreate(final Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.blah);
   WebView webview = (WebView)findViewById(R.id.webview);
   if (savedInstanceState != null)
      webview.restoreState(savedInstanceState);
   else
      webview.loadUrl(URLData)
}