Restore webview scroll position?


I want to <strong>save the state</strong> of my webView with its <strong>page scroll position</strong> when user leaves the app and restore them when user opens the app again. So that, user can continue reading the restored webview content scrolled down to the restored position.

Here are methods I'm using:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_book); webView = (WebView)this.findViewById(R.id.webView); if (savedInstanceState != null) { int positionY = Math.round(scrollPos); webView.scrollTo(0, positionY); } esle { //do something }

Function that calculates the position:

private int calculateProgression(WebView content) { int positionTopView = content.getTop(); int contentHeight = content.getContentHeight(); int currentScrollPosition = content.getScrollY(); int percentWebview = (currentScrollPosition - positionTopView) / contentHeight; return percentWebview; }

save/restore functions:

@Override protected void onSaveInstanceState(Bundle outState) { outState.putInt("position", calculateProgression(webView)); super.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); scrollPos = savedInstanceState.getFloat("position"); }


You could save the position in the <a href="https://developer.android.com/reference/android/content/SharedPreferences.html" rel="nofollow">SharedPreferences</a> in the OnDestroy method and retrieve it in the OnCreate method.

<strong>EDIT</strong>: As stated <a href="https://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29" rel="nofollow">here</a>, OnPause should be the place to store data, not OnDestroy.


  • How to add an item to a ListView with each Button click
  • Where are files stored on an Android emulator?
  • Is Android's ARGB_8888 Bitmap internal format always RGBA?
  • android-How to enable cache or buffering when videoView have downloaded a video
  • OnItemSelectedListener for Spinner that was added programmatically does not trigger
  • Listview - list item selection and initial setting (custom made items)
  • Replace Fragment with another on back button
  • Avoid registering duplicate broadcast receivers in Android
  • How to get listview position?
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Bypass multiple inheritance in Java
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • Connect .sks to skscene.h
  • how to avoid repetitive constructor in children
  • How to test if a URL from an Eclipse bundle is a directory?
  • Recording logins for password protected directories
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Deleting and Updating values from a cusrsor adapter
  • Azure Cloud Service Web Role web pages do not load
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • C# - Getting references of reference
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • How to stop GridView from loading again when I press back button?
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Is there any way to bind data to data.frame by some index?
  • Checking variable from a different class in C#
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?