Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dot indicator resets to the first page after the activity is rotated #126

Closed
nachogoro opened this issue May 21, 2021 · 5 comments
Closed
Assignees
Labels
Milestone

Comments

@nachogoro
Copy link

nachogoro commented May 21, 2021

As the title states. I set the ViewPager2 object for the SpringDotsIndicator in the onCreate() method of my activity, and don't do anything else with the SpringDotsIndicator:

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        ...
        final ViewPager2 viewPager = findViewById(R.id.tutorial_pager);
        final FragmentStateAdapter pageAdapter = new MyPageAdapter(this);
        viewPager.setAdapter(pageAdapter);
        final SpringDotsIndicator dotsIndicator = (SpringDotsIndicator) findViewById(R.id.spring_dots_indicator);
        dotsIndicator.setViewPager2(viewPager);
        ...

It behaves correctly normally. However, if I'm on e.g. my third fragment and rotate the screen, when the activity is recreated the fragment shown in the new orientation is still the third one, but the dot indicates that we are on the first fragment.

As soon as I slide the fragment slightly the dot catches up and goes to the correct position.

Is this a bug or am I missing some code in order to handle these cases?

@nachogoro
Copy link
Author

The issue is still unresolved, but I did a bit more digging.

The current item of the ViewPager2 object is reset after a screen rotation, due to the activity being recreated, see this question in StackOverflow. Indeed, I checked getCurrentItem() in my original onStart() method and it was reporting 0 regardless of the fragment being shown.

However, I have fixed this issue by saving the current item as the original activity is being destroyed and restoring it in my onCreate(final Bundle bundle) method, and the issue still persists. The ViewPager2 object now correctly reports the current item after the rotation, but the dot is still in the first position.

    private ViewPager2 mViewPager;
    private SpringDotsIndicator mDotsIndicator;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        ...
        viewPager = findViewById(R.id.tutorial_pager);
        final FragmentStateAdapter pageAdapter = new MyPageAdapter(this);
        viewPager.setAdapter(pageAdapter);
        if (savedInstanceState != null) {
            final int position = savedInstanceState.getInt("current_position");
            mViewPager.setCurrentItem(position);
        }
        mDotsIndicator = (SpringDotsIndicator) findViewById(R.id.spring_dots_indicator);
        mDotsIndicator.setViewPager2(mViewPager);
        ...
    }

    @Override
    public void onSaveInstanceState(final @NonNull Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putInt("current_position", mViewPager.getCurrentItem());
    }

I can see that I'm setting the right position in the ViewPager2 object in onCreate() and that it remains correct in onStart(). For some reason, though, it doesn't seem to be picked up by the SpringDotsIndicator object. Invoking refreshDots() in onStart() also has no effect.

@nachogoro
Copy link
Author

The issue can be reproduced easily with the provided sample. Surprisingly, it seems to happen with the SpringDotsIndicator but not with the WormDotsIndicator.

@nachogoro
Copy link
Author

It looks like this issue has been reported several times. The best explanation I've found is by @krodyrobi here (issue #57 ).

Since issue 57 is a fairly old issue, I was wondering if there are any plans yo look into this @tommybuonomo

Cheers and thanks for the nice library :)

@tommybuonomo tommybuonomo self-assigned this Apr 30, 2022
@tommybuonomo tommybuonomo added this to the 4.3 milestone Apr 30, 2022
@tommybuonomo
Copy link
Owner

Hello @nachogoro 👋
I am sorry for this very late reply, but this issue will be fixed in the coming 4.3 version

@tommybuonomo
Copy link
Owner

Hello, thanks for your contribution !
This issue is now fixed in the new version 4.3
Thanks 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants