views:

690

answers:

1

I have a problem preserving the current scroll position of content loaded in Android's WebView control during orientation changes. The issue currently exist only in Android 2.1 and Android 2.1 Update 1

The code that I use is based on the default Browser application and looks like this:

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  mWebView = new WebView(this);
  setContentView(mWebView);
  mWebView.setWebViewClient(mWebViewClient);

  if (savedInstanceState != null)
  {
    final WebBackForwardList list = mWebView.restoreState(savedInstanceState);
    if (list == null)
      return;

    if (savedInstanceState.containsKey("currentPicture"))
    {
       final File f = new File(savedInstanceState.getString("currentPicture"));
       mWebView.restorePicture(savedInstanceState, f);
       f.delete();
    }
  }else
    mWebView.loadUrl("http://slashdot.org/");
 }

 @Override
 protected void onSaveInstanceState(Bundle outState)
 {
   final WebBackForwardList list = mWebView.saveState(outState);
   File mThumbnailDir = getDir("thumbnails", 0);
   if (list != null)
   {
     final File f = new File(mThumbnailDir, mWebView.hashCode() + "_pic.save");
     if (mWebView.savePicture(outState, f))
        outState.putString("currentPicture", f.getPath());
   }
 }

This code works in Android 1.5, 1.6 and 2.0.1. In Android 2.1 the same code restores the position for a split second but then the page reloads and scrolls to position 0,0.

In Android 2.1 Update 1, it not only scrolls back to 0,0, but also changes the Zoom to fit the page width (similar to the browser Overview mode introduced in 2.1)

Does anyone know of a workaround or has any idea what may cause such behavior?

A: 

Hi

I am also facing a similar kind of issue with Android 2.1. In default browser when we open any page such as google and the change orientation, then from to portrait to landscape some scrolling happens. When I come back to portrait I observe that the page has scrolled though there was no scrolling done by the user while in landscape mode.

Please provide your feedback if anybody knows how to fix the issue.

Dheeraj