Skip to content

Commit

Permalink
SaveInstanceState. issue #175
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip committed Dec 8, 2020
1 parent db01393 commit e4e301c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.ColorInt;
Expand All @@ -13,6 +14,7 @@
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -415,6 +417,31 @@ private void refreshIndicator(List<? extends T> data) {
mIndicatorView.notifyDataChanged();
}

private static final String KEY_SUPER_STATE = "SUPER_STATE";
private static final String KEY_CURRENT_POSITION = "CURRENT_POSITION";
private static final String KEY_IS_CUSTOM_INDICATOR = "IS_CUSTOM_INDICATOR";

@Nullable
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
Bundle bundle = new Bundle();
bundle.putParcelable(KEY_SUPER_STATE, superState);
bundle.putInt(KEY_CURRENT_POSITION, currentPosition);
bundle.putBoolean(KEY_IS_CUSTOM_INDICATOR, isCustomIndicator);
return bundle;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
Bundle bundle = (Bundle) state;
Parcelable superState = bundle.getParcelable(KEY_SUPER_STATE);
super.onRestoreInstanceState(superState);
currentPosition = bundle.getInt(KEY_CURRENT_POSITION);
isCustomIndicator = bundle.getBoolean(KEY_IS_CUSTOM_INDICATOR);
setCurrentItem(currentPosition);
}

private int getInterval() {
return mBannerManager.getBannerOptions().getInterval();
}
Expand Down

0 comments on commit e4e301c

Please sign in to comment.