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

Be careful with findFragmentById() to find fragments inside a FragmentStatePagerAdapter #3

Open
ghost opened this issue Jan 5, 2016 · 1 comment

Comments

@ghost
Copy link

ghost commented Jan 5, 2016

Hi,

I found your blog post while searching for my own bug with FragmentStatePagerAdapter.

So I'll share my experience...

You can test this bug on support library 23.1.1:

  • Put a ViewPager on your activity and use a FragmentStatePagerAdapter as adapter, which provides a list of fragments.
  • Each fragment saves its own index within the adapter in its arguments (you provide that index).
  • On each fragment, put a button. When a button is clicked, you get the fragment manager, and call findFragmentById() with the current fragment ID (via getId()).
  • Take the result fragment, get the index back to compare it to current fragment index.

The bug is: findFragmentById() returns random fragment. The index number is wrong in most case.

So be careful :-)

@adamsp
Copy link
Owner

adamsp commented Jan 5, 2016

Thanks for the tip. I haven't touched this in years, but I'll leave this open so hopefully anyone else who comes across this can benefit from your experiences.

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

No branches or pull requests

1 participant