diff --git a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java index 2e7c732..1db9a2d 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java @@ -99,6 +99,7 @@ public class BannerViewPager extends RelativeLayout implements LifecycleObser private Path mRadiusPath; private int startX, startY; + private Lifecycle lifecycleRegistry; private final ViewPager2.OnPageChangeCallback mOnPageChangeCallback = new ViewPager2.OnPageChangeCallback() { @@ -513,8 +514,13 @@ public List getData() { * Start loop */ public void startLoop() { - if (!isLooping && isAutoPlay() && mBannerPagerAdapter != null && - mBannerPagerAdapter.getListSize() > 1 && isAttachedToWindow()) { + if (!isLooping + && isAutoPlay() + && mBannerPagerAdapter != null + && + mBannerPagerAdapter.getListSize() > 1 + && isAttachedToWindow() + && lifecycleRegistry.getCurrentState() == Lifecycle.State.RESUMED) { mHandler.postDelayed(mRunnable, getInterval()); isLooping = true; } @@ -1067,12 +1073,13 @@ public BannerViewPager registerOnPageChangeCallback( */ @Deprecated public BannerViewPager setLifecycleRegistry(Lifecycle lifecycleRegistry) { - lifecycleRegistry.addObserver(this); + registerLifecycleObserver(lifecycleRegistry); return this; } public BannerViewPager registerLifecycleObserver(Lifecycle lifecycleRegistry) { lifecycleRegistry.addObserver(this); + this.lifecycleRegistry = lifecycleRegistry; return this; } @@ -1088,9 +1095,7 @@ public void onPause() { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public void onResume() { - if (!isStopLoopWhenDetachedFromWindow()) { - startLoop(); - } + startLoop(); } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)