Restore WebView position to same content when orientation changes (Android)

I ran into a problem with WebView content scrolling content, and here it is: I drop the page a bit in portrait mode, and when I change the orientation to the landscape, the scroll moves up or down. It does not show the exact content that is in portrait mode. I tried making changes to the manifest file as configChanges="keyboardHidden|orientation|screenSize" but no luck. I overridden onConfigurationChanged() , but that didn't work. I implemented the accepted answer in question , but to no avail. Can anyone suggest what needs to be done and why it doesn't work? This is for the Google Nexus tab, and the minimum version of the SDK is 14. Thanks to everyone.

+6
source share
3 answers

I remember having the same problem with my application. If I'm not mistaken, the solution suggested by Phil doesn't work (on some versions of Android?).

Now I use the following approach:

  • in onSaveInstanceState, save the current scroll position of the WebView: outState.putInt(EXTRA_SCROLL_POSITION, webView.getScrollY());
  • in onCreate, get the last scroll position from the saved InstanceState and set your own WebViewClient:

     setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); if (!scrolled) { postDelayed(new Runnable() { @Override public void run() { scrollBy(0, scroll); } }, 250); scrolled = true; } } }); 

It's ugly, damn it, but it works for me (it doesn't take into account the current zoom level, etc.). There you can improve a lot about this code, but basically it is a trick.

+2
source

Class classes have an onSaveInstanceState() method that returns the Parcelable and onRestoreInstanceState(Parcelable state) to restore state. If I remember correctly, the saved state includes the scroll position. All you have to do is save the Parcelable that you get from calling WebView onSaveInstanceState() in the onSaveInstanceState fragment onSaveInstanceState :

 public void onSaveInstanceState(Bundle savedState) { super.onSaveInstanceState(savedState); WebView webView = (WebView) findViewById( ... ); savedState.putParcelable("myKey", webView.onSaveInstanceState()); } 

Then, in your onCreateView method onCreateView you restore this state, if available:

 public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... if (savedInstanceState != null) { WebView webView = (WebView) findViewById( ... ); Parcelable savedState = savedInstanceState.getParcelable("myKey"); if (savedState != null) { webView.onRestoreInstanceState(savedState); } } .... } 
+1
source

Try using onSaveInstanceState to keep the scrollview state and restore the onRestoreInstanceState method . Use Scrollview thr xml ? if yes , then get a link to this view and when the activity is restored , then set the scrollview > position to the saved state. Hope this helps.

0
source

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


All Articles