Skip to content

Commit c65d361

Browse files
authored
Merge pull request umano#759 from srMarlins/feature/srmarlins/SavedInstanceState
Refactors the way savedinstancestate works to avoid BadParceableExcep…
2 parents b39b196 + 2b8b3de commit c65d361

File tree

1 file changed

+17
-50
lines changed

1 file changed

+17
-50
lines changed

library/src/main/java/com/sothree/slidinguppanel/SlidingUpPanelLayout.java

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import android.graphics.Rect;
1010
import android.graphics.drawable.Drawable;
1111
import android.os.Build;
12+
import android.os.Bundle;
1213
import android.os.Parcel;
1314
import android.os.Parcelable;
1415
import android.support.annotation.NonNull;
16+
import android.support.v4.app.BundleCompat;
1517
import android.support.v4.view.MotionEventCompat;
1618
import android.support.v4.view.ViewCompat;
1719
import android.util.AttributeSet;
@@ -76,6 +78,10 @@ public class SlidingUpPanelLayout extends ViewGroup {
7678
private static final int[] DEFAULT_ATTRS = new int[]{
7779
android.R.attr.gravity
7880
};
81+
/**
82+
* Tag for the sliding state stored inside the bundle
83+
*/
84+
public static final String SLIDING_STATE = "sliding_state";
7985

8086
/**
8187
* Minimum velocity that will be detected as a fling
@@ -1298,22 +1304,21 @@ public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
12981304

12991305
@Override
13001306
public Parcelable onSaveInstanceState() {
1301-
Parcelable superState = super.onSaveInstanceState();
1302-
1303-
SavedState ss = new SavedState(superState);
1304-
if (mSlideState != PanelState.DRAGGING) {
1305-
ss.mSlideState = mSlideState;
1306-
} else {
1307-
ss.mSlideState = mLastNotDraggingSlideState;
1308-
}
1309-
return ss;
1307+
Bundle bundle = new Bundle();
1308+
bundle.putParcelable("superState", super.onSaveInstanceState());
1309+
bundle.putSerializable(SLIDING_STATE, mSlideState != PanelState.DRAGGING ? mSlideState : mLastNotDraggingSlideState);
1310+
return bundle;
13101311
}
13111312

13121313
@Override
13131314
public void onRestoreInstanceState(Parcelable state) {
1314-
SavedState ss = (SavedState) state;
1315-
super.onRestoreInstanceState(ss.getSuperState());
1316-
mSlideState = ss.mSlideState != null ? ss.mSlideState : DEFAULT_SLIDE_STATE;
1315+
if(state instanceof Bundle) {
1316+
Bundle bundle = (Bundle) state;
1317+
mSlideState = (PanelState) bundle.getSerializable(SLIDING_STATE);
1318+
mSlideState = mSlideState == null ? DEFAULT_SLIDE_STATE : mSlideState;
1319+
state = bundle.getParcelable("superState");
1320+
}
1321+
super.onRestoreInstanceState(state);
13171322
}
13181323

13191324
private class DragHelperCallback extends ViewDragHelper.Callback {
@@ -1453,42 +1458,4 @@ public LayoutParams(Context c, AttributeSet attrs) {
14531458
ta.recycle();
14541459
}
14551460
}
1456-
1457-
static class SavedState extends BaseSavedState {
1458-
PanelState mSlideState;
1459-
1460-
SavedState(Parcelable superState) {
1461-
super(superState);
1462-
}
1463-
1464-
private SavedState(Parcel in) {
1465-
super(in);
1466-
String panelStateString = in.readString();
1467-
try {
1468-
mSlideState = panelStateString != null ? Enum.valueOf(PanelState.class, panelStateString)
1469-
: PanelState.COLLAPSED;
1470-
} catch (IllegalArgumentException e) {
1471-
mSlideState = PanelState.COLLAPSED;
1472-
}
1473-
}
1474-
1475-
@Override
1476-
public void writeToParcel(Parcel out, int flags) {
1477-
super.writeToParcel(out, flags);
1478-
out.writeString(mSlideState == null ? null : mSlideState.toString());
1479-
}
1480-
1481-
public static final Parcelable.Creator<SavedState> CREATOR =
1482-
new Parcelable.Creator<SavedState>() {
1483-
@Override
1484-
public SavedState createFromParcel(Parcel in) {
1485-
return new SavedState(in);
1486-
}
1487-
1488-
@Override
1489-
public SavedState[] newArray(int size) {
1490-
return new SavedState[size];
1491-
}
1492-
};
1493-
}
14941461
}

0 commit comments

Comments
 (0)