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; } }