when i use backup & restore Webview state ,i recive this message : the webpage at x Address might be temporarity down or may have moved permanently to a new web address.
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
// showAlertDialog(HomeScreen.this, "Internet Connection",
// "You have internet connection", true);
webviewbrowse.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webviewAds.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webviewbrowse.loadUrl("http://www.example.com");
} else {
// Internet connection is not present
// Ask user to connect to Internet
webviewbrowse.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webviewAds.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webviewbrowse.loadUrl("http://example.com");
showAlertDialog(HomeScreen.this, "internet doesn't connect",
" please connect to internet", false);
}
restoreState was never reliable. That's perhaps why the documentation now says this.
If it is called after this WebView has had a chance to build state (load pages, create a back/forward list, etc.) there may be undesirable side-effects. Please note that this method no longer restores the display data for this WebView.
And the corresponding entry for saveState() speaks thus:
Please note that this method no longer stores the display data for this WebView.
what you really should do inside the onCreate method is to call webView.loadUrl() if you want to display the last visited url, please see this answer:
If you are concerned about the webview reloading on orientation change etc.
you can set your Activity to handle the orientation and keyboardHidden changes, and then just leave the WebView alone
binding.webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
);
binding.webView.getSettings().setJavaScriptEnabled(true);
binding.webView.getSettings().setBuiltInZoomControls(true);
binding.webView.getSettings().setDisplayZoomControls(false);
binding.webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 );
binding.webView.getSettings().setAppCachePath( getActivity().getApplicationContext().getCacheDir().getAbsolutePath() );
binding.webView.getSettings().setAllowFileAccess( true );
binding.webView.getSettings().setAppCacheEnabled( true );
binding.webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
if ( !ConnectivityStatus.Companion.isConnected(getContext()) ) { // loading offline
binding.webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}
binding.webView.loadUrl(this.url);
If your WebView is in a Fragment, please check below:
saving:
@Override
public void onSaveInstanceState(Bundle outState) {
webView.saveState(outState); // output would be a WebBackForwardList
}
You may check the doc for saveState.
restoring:
@Override
public void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState != null) {
webView.restoreState(savedInstanceState);
} else {
webView.loadUrl("http://mypage");
}
}