Faced such a problem:

When I switch from 1st page to another in WebView, and then I return with the back button, 1st page is loaded from the very beginning.

This is a petty problem, but if I flipped through the photo gallery through WebView, switched to a photo and returned to the photo gallery, it becomes very inconvenient, you need to rewind from the very beginning to the place where I stopped.

Is there any customization to the webview so that it retains the last position on the previous page?

This line tried to load with cache priority, did not work ..

webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

The code itself:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_foto, container, false); webView = (WebView)v.findViewById(R.id.webView); webView.setWebViewClient(new MyWebViewClient()); webView.getSettings().setJavaScriptEnabled(true); // вкл яваскрипта webView.getSettings().setBuiltInZoomControls(true); // встроенные кнопки вебвью webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //приоритет загрузки сначала с кэша if (isOnline()) { //если метод проверил смартфон на соединение с интернетом, выполним загрузку webView.loadUrl("https://m.facebook.com/page/mediaset.php?id=809332342448482&album=pb.809332342448482.-2207520000.1459176546.&refid=17&ref=page_internal"); // метод для перехода назад постранично, т.к. webView во фрагменте(фрагментов много, активити один) webView.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { handler.sendEmptyMessage(1); return true; } return false; } }); }else Toast.makeText(getActivity(), "Нет соединения", Toast.LENGTH_LONG);//если метод проверил смартфон на отрицательное соединение с интернетом return v; } 

UPD: Sorry, here's another method that I use

 private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } 

    1 answer 1

    Try saving the WebView state to the Bundle .

    In the onCreateView () add the following lines:

     if (savedInstanceState != null) webview.restoreState(savedInstanceState); else webview.loadUrl("https://m.facebook.com/page/mediaset.php?id=809332342448482&album=pb.809332342448482.-2207520000.1459176546.&refid=17&ref=page_internal") 

    And override onSaveInstanceState() :

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

    And onViewStateRestored() :

     @Override public void onViewStateRestored(Bundle savedInstanceState) { super.onViewStateRestored(savedInstanceState); webView.restoreState(savedInstanceState); } 
    • It did not work .. And if with your code, some of my lines turned out to be superfluous, I don’t even know what can conflict with and what can be removed - zayn1991
    • @ zayn1991 yes here, in principle, in particular, and no where to clash ... but what, in general, the behavior has not changed? - ermak0ff
    • when returning to the previous page, the page is 5-10 lines still lower than it was without your methods, but this is almost not noticeable - zayn1991
    • one
      right now I won’t say exactly, but you just need to store webview.getScollY (). And when webview loads the page, call webView.ScrollTo (y), where Y is the same saved value - Android Android
    • @ ermak0ff everything, I figured out, since I use the facebook link, and they have paginal uploading of photos in my link, so the situation could not be remembered and therefore your method did not work correctly for me. On other pages, your method works. This is already a Facebook feature, so I’ll choose this answer, since for regular pages it’s a worker - zayn1991