Android webView saveState

I have a tabhost with 3 tabs. Each of these tabs has a web view. When I click the tab, the webview needs to be โ€œreloadedโ€, even when I was there before, it was not saved. Is there a way to save web browsing?

+15
android save webview
Apr 21 '12 at 12:12
source share
3 answers

This can be handled by overrwriting onSaveInstanceState (Bundle outState) in your activity and calling saveState from a web browser:

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

Then restore this to 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) } 

you can reference this link

+33
Apr 21 '12 at 12:13
source share

restoreState has never been reliable. And by 2015, the documents accept the fact. (Perhaps the change was made to the documents around 2014). This is what he is saying now.

If it is called after this WebView has the ability to build state (load pages, create a list back / forward, etc.), there may be unwanted side effects. Note that this method no longer works restores display data for this WebView.

And the corresponding entry for saveState () says this:

Note that this method no longer saves the display data for this WebView.

What you really need to do inside the onCreate method is to call webView.loadUrl () if you want to display the last visited URL, see this answer :

If you are concerned about restarting webview when changing orientation, etc.

you can set your activity to handle orientation and changes using the Hidden keyboard and then just leave only the WebView

+8
Sep 30 '15 at 13:38
source share

Alternatively, you can examine the fragments using tabHost. when you enter another webView, you can set a fragment to hide and another to show. when you return to the previous fragment, the content will not be destroyed. The way I did it and it worked for me.

+1
Oct 09 '13 at 11:26
source share



All Articles