Skip to content

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

@ghost

Description

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 :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions