Permalink
Browse files

Added code to save the current screen to handle orientation changes o…

…r any other behaviors where the activity would be destroyed.
  • Loading branch information...
Accused187 committed May 20, 2011
1 parent 531474b commit 9c9df825cf6ee9df9dd8bda84994fee7d5d453e6
Showing with 48 additions and 0 deletions.
  1. +48 −0 src/com/github/ysamlan/horizontalpager/HorizontalPager.java
@@ -138,6 +138,22 @@ private void init() {
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
+ @Override
+ protected Parcelable onSaveInstanceState() {
+ final SavedState state = new SavedState(super.onSaveInstanceState());
+ state.mCurrentScreen = mCurrentScreen;
+ return state;
+ }
+
+ @Override
+ protected void onRestoreInstanceState(Parcelable state) {
+ SavedState savedState = (SavedState) state;
+ super.onRestoreInstanceState(savedState.getSuperState());
+ if (savedState.mCurrentScreen != -1) {
+ mCurrentScreen = savedState.mCurrentScreen;
+ }
+ }
+
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
@@ -501,4 +517,36 @@ private void snapToScreen(final int whichScreen, final int duration) {
*/
void onScreenSwitched(int screen);
}
+
+ /**
+ * A parcelable so we can save our current screen and return to it after an activity destroy.
+ */
+ public static class SavedState extends BaseSavedState {
+ int mCurrentScreen = -1;
+
+ SavedState(Parcelable superState) {
+ super(superState);
+ }
+
+ private SavedState(Parcel in) {
+ super(in);
+ mCurrentScreen = in.readInt();
+ }
+
+ @Override
+ public void writeToParcel(Parcel out, int flags) {
+ super.writeToParcel(out, flags);
+ out.writeInt(mCurrentScreen);
+ }
+
+ public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
+ public SavedState createFromParcel(Parcel in) {
+ return new SavedState(in);
+ }
+
+ public SavedState[] newArray(int size) {
+ return new SavedState[size];
+ }
+ };
+ }
}

1 comment on commit 9c9df82

@ysamlan

This comment has been minimized.

Show comment Hide comment
@ysamlan

ysamlan Jun 14, 2011

This code doesn't seem to actually work (try running the demo app and rotating the screen), on either the simple or tabbed examples. Can you confirm that the demos work for you with your patch?

This code doesn't seem to actually work (try running the demo app and rotating the screen), on either the simple or tabbed examples. Can you confirm that the demos work for you with your patch?

Please sign in to comment.