How to save and restore web browsing state?

when I use Webview backup and restore, I get this message: a web page with address x may be temporarily unavailable or may be constantly being moved to a new web address.

@Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); webViewShowPoll.saveState(savedInstanceState); } @Override public void onRestoreInstanceState(Bundle outState) { super.onRestoreInstanceState(outState); webViewShowPoll.restoreState(outState); } 

and Androidmanifest.xml

 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.SET_DEBUG_APP"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <activity android:name="com.omid.epoll.mobile.Poll" android:launchMode="singleInstance" android:label="@string/title_activity_poll" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"> </activity> 
+4
source share
4 answers

SAVING WEBVIEW EVENTS

 public class Tab2Fragment extends Fragment { private WebView webView; private Bundle webViewBundle; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.tab2, container, false); webView = (WebView) ll.findViewById(R.id.webView1); webView.setWebViewClient(new WebViewClient()); if (webViewBundle == null) { webView.loadUrl("http://www.lucazanini.eu"); } else { webView.restoreState(webViewBundle); } return ll; } @Override public void onPause() { super.onPause(); webViewBundle = new Bundle(); webView.saveState(webViewBundle); } } 
+7
source
  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); } 
+2
source

restoreState has never been reliable. Perhaps this is why the documentation now talks about it.

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 calling webView.loadUrl () if you want to display the last url you visit, 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

+2
source

If your WebView is in a snippet, check below:

Saving:

 @Override public void onSaveInstanceState(Bundle outState) { webView.saveState(outState); // output would be a WebBackForwardList } 

You can check the document for saveState.

recovery:

 @Override public void onCreate(Bundle savedInstanceState) { ... if (savedInstanceState != null) { webView.saveState(savedInstanceState); } else { webView.loadUrl("http://mypage"); } } 
0
source

Source: https://habr.com/ru/post/1499295/


All Articles