Qassim babyDroid Qassim babyDroid - 8 months ago 51
Android Question

Restore webview scroll position?

I want to save the state of my

with its page scroll position 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:

protected void onCreate(Bundle savedInstanceState) {
webView = (WebView)this.findViewById(;
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:

protected void onSaveInstanceState(Bundle outState) {
outState.putInt("position", calculateProgression(webView));

protected void onRestoreInstanceState(Bundle savedInstanceState) {
scrollPos = savedInstanceState.getFloat("position");


You could save the position in the SharedPreferences in the OnDestroy method and retrieve it in the OnCreate method.

EDIT: As stated here, OnPause should be the place to store data, not OnDestroy.