From 8c8258878610c0b34b45bd05a5b2080efbb74ea6 Mon Sep 17 00:00:00 2001 From: jonatansalas Date: Sun, 22 Jan 2017 20:33:42 -0300 Subject: [PATCH] #32 added support to restore currentMonthIndex --- .../view/CalendarView.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/materialcalendarview/src/main/java/io/blackbox_vision/materialcalendarview/view/CalendarView.java b/materialcalendarview/src/main/java/io/blackbox_vision/materialcalendarview/view/CalendarView.java index d097304..732d667 100644 --- a/materialcalendarview/src/main/java/io/blackbox_vision/materialcalendarview/view/CalendarView.java +++ b/materialcalendarview/src/main/java/io/blackbox_vision/materialcalendarview/view/CalendarView.java @@ -8,6 +8,7 @@ import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.os.Build; +import android.os.Bundle; import android.os.Parcelable; import android.support.annotation.ColorRes; import android.support.annotation.DrawableRes; @@ -59,6 +60,9 @@ public final class CalendarView extends LinearLayout { private static final Interpolator DEFAULT_ANIM_INTERPOLATOR = new DecelerateInterpolator(3.0f); private static final long DEFAULT_ANIM_DURATION = 1500; + private static final String KEY_STATE = "superState"; + private static final String KEY_MONTH_INDEX = "currentMonthIndex"; + private static final int SUNDAY = 1; private static final int MONDAY = 2; private static final int TUESDAY = 4; @@ -233,11 +237,26 @@ public CalendarView(Context context, AttributeSet attrs) { @Override protected Parcelable onSaveInstanceState() { - return super.onSaveInstanceState(); + final Parcelable superState = super.onSaveInstanceState(); + final Bundle stateToSave = new Bundle(); + + stateToSave.putParcelable(KEY_STATE, superState); + stateToSave.putInt(KEY_MONTH_INDEX, currentMonthIndex); + + return stateToSave; } @Override protected void onRestoreInstanceState(Parcelable state) { + if (state instanceof Bundle) { + final Bundle savedInstanceState = (Bundle) state; + + state = savedInstanceState.getParcelable(KEY_STATE); + currentMonthIndex = savedInstanceState.getInt(KEY_MONTH_INDEX); + + update(Calendar.getInstance(Locale.getDefault())); + } + super.onRestoreInstanceState(state); }