OSDN Git Service

Don't save ScrollView/HorizontalScrollView saved state for old apps
authorAdam Powell <adamp@google.com>
Fri, 14 Jun 2013 00:44:04 +0000 (17:44 -0700)
committerAdam Powell <adamp@google.com>
Fri, 14 Jun 2013 00:44:04 +0000 (17:44 -0700)
Older apps may have given IDs to ScrollViews and views of a different
class in layouts that vary by configuration. Now that ScrollViews save
state this causes ClassCastExceptions when trying to restore instance
state.

Only save new instance state for ScrollView/HorizontalScrollView for
apps targeting post-API 18.

Change-Id: Icaa095cd20bef35dddc225a17c5a8e30b3faea02

core/java/android/widget/HorizontalScrollView.java
core/java/android/widget/ScrollView.java

index c89c91e..f0d80e6 100644 (file)
@@ -20,6 +20,7 @@ import android.content.Context;
 import android.content.res.TypedArray;
 import android.graphics.Canvas;
 import android.graphics.Rect;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -1467,7 +1468,7 @@ public class HorizontalScrollView extends FrameLayout {
         mIsLayoutDirty = false;
         // Give a child focus if it needs it
         if (mChildToScrollTo != null && isViewDescendantOf(mChildToScrollTo, this)) {
-                scrollToChild(mChildToScrollTo);
+            scrollToChild(mChildToScrollTo);
         }
         mChildToScrollTo = null;
 
@@ -1641,6 +1642,12 @@ public class HorizontalScrollView extends FrameLayout {
 
     @Override
     protected void onRestoreInstanceState(Parcelable state) {
+        if (mContext.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
+            // Some old apps reused IDs in ways they shouldn't have.
+            // Don't break them, but they don't get scroll state restoration.
+            super.onRestoreInstanceState(state);
+            return;
+        }
         SavedState ss = (SavedState) state;
         super.onRestoreInstanceState(ss.getSuperState());
         mSavedState = ss;
@@ -1649,6 +1656,11 @@ public class HorizontalScrollView extends FrameLayout {
 
     @Override
     protected Parcelable onSaveInstanceState() {
+        if (mContext.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
+            // Some old apps reused IDs in ways they shouldn't have.
+            // Don't break them, but they don't get scroll state restoration.
+            return super.onSaveInstanceState();
+        }
         Parcelable superState = super.onSaveInstanceState();
         SavedState ss = new SavedState(superState);
         ss.scrollPosition = mScrollX;
index b764c98..b32cfbc 100644 (file)
@@ -16,6 +16,7 @@
 
 package android.widget;
 
+import android.os.Build;
 import android.os.Parcel;
 import android.os.Parcelable;
 import com.android.internal.R;
@@ -1662,6 +1663,12 @@ public class ScrollView extends FrameLayout {
 
     @Override
     protected void onRestoreInstanceState(Parcelable state) {
+        if (mContext.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
+            // Some old apps reused IDs in ways they shouldn't have.
+            // Don't break them, but they don't get scroll state restoration.
+            super.onRestoreInstanceState(state);
+            return;
+        }
         SavedState ss = (SavedState) state;
         super.onRestoreInstanceState(ss.getSuperState());
         mSavedState = ss;
@@ -1670,6 +1677,11 @@ public class ScrollView extends FrameLayout {
 
     @Override
     protected Parcelable onSaveInstanceState() {
+        if (mContext.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
+            // Some old apps reused IDs in ways they shouldn't have.
+            // Don't break them, but they don't get scroll state restoration.
+            return super.onSaveInstanceState();
+        }
         Parcelable superState = super.onSaveInstanceState();
         SavedState ss = new SavedState(superState);
         ss.scrollPosition = mScrollY;