GithubHelp home page GithubHelp logo

adamsp / fragmentstatepagerissueexample Goto Github PK

View Code? Open in Web Editor NEW
30.0 30.0 11.0 376 KB

Example of an issue around state restoration for fragments in a FragmentStatePagerAdapter

Groovy 5.57% Java 94.43%

fragmentstatepagerissueexample's People

Contributors

adamsp avatar rlac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fragmentstatepagerissueexample's Issues

Additional check in "instantiateItem"

In instantiateItem, we need to add additional check to make sure the fragment retrieved from mFragments is correct.

String fragmentTag = getTag(position); 
if (mFragments.size() > position) {
    Fragment f = mFragments.get(position);
    if (f != null && TextUtils.equals(f.getTag(), fragmentTag)) {
        return f;
    }
}

This is because calling notifyDataSetChanged(), eventually calling getItemPosition(), does not remove the fragment at the end of list.

In my case, A(0), B(1), C(2) are initiated. When deleting B(1), I animate viewpager to move current item to C(2) first. This makes mFragments to be A-destroyed(0), B(1), C-currentItem(2), D-initiated(3).

I then call notifyDataSetChanged() and this return POSITION_NONE for B(1). This updates mFragments to be A-recalled(0), C-currentItem(1), D-reinserted(2), D-not deleted(3). So when I swipe right, a new fragment E(3) was not created. Fragment D-not deleted was recalled and it gave a black screen in viewpager.

Solution without duplicating library source code

Hi I found your blog from Google, but I didn't really like the idea of maintaining a separate FragmentStatePagerAdapter. A simpler solution is to use FragmentPagerAdapter instead, and override the getItemId() and getItemPosition() methods.

See here for some source code

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

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.