AKroell
AKroell

Reputation: 622

FragmentStatePagerAdapter items don't load

In my main activity I have multiple Fragments that you can navigate through a bottom navigation (Replace a FrameLayout container in main activity XML)

One of those Fragments contains a ViewPager that should show multiple Fragments (MyFragment) using a FragmentStatePagerAdapter (MyAdapter).

First time I open that Fragment it works just fine but when I navigate to a different one and then come back the first two Fragments in the Pager are grey and unpopulated. Anyone got an idea what the problem might be?

The Adapter is set up like this: This is called in onFragmentViewCreated

private void setUpHeaderPager() {
    featuredAdapter = new MyAdapter(getActivity().getSupportFragmentManager());
    myPager.setAdapter(myAdapter);
    pageIndicatorView.setViewPager(myPager);
    pageIndicatorView.setAnimationType(AnimationType.WORM);
    compositeSubscription.add(apiService.getMyPOJOs()
            .subscribeOn(networkScheduler)
            .observeOn(uiScheduler)
            .subscribe(new Action1<List<MyPOJO>>() {
                @Override
                public void call(List<MyPOJO> myPOJOs) {
                    myAdapter.setItems(myPOJOs);
                    pageIndicatorView.setCount(myPOJOs.size());
                    myPager.setCurrentItem(0);
                    pageIndicatorView.setSelection(0);
                }
            }, new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {
                    throwable.printStackTrace();
                }
            }));
}

And this is what the Adapter looks like:

private class MyAdapter extends FragmentStatePagerAdapter {

    private List<SomePOJO> items = new ArrayList<>(0);

    public void setItems(@Nonnull List<Collection> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    FeaturedReleasesAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public Fragment getItem(int position) {
        return MyFragment.newInstance(items.get(position));
    }

    @Override
    public int getCount() {
        return items.size();
    }
}

Just in case the relevant code of MyFragment:

public class FeaturedReleaseFragment extends BaseFragment {

private MyPOJO someObject = null;

// Bind some views

public static MyFragment newInstance(MyPOJO someObject) {
    MyFragment fragment = new MyFragment();
    fragment.someObject = someObject;
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_my_fragment, container, false);

    ButterKnife.bind(this, rootView);

    populate();

    return rootView;
}

private void populate() {
    if (MyPOJO != null) {
        // set some text and load images
    }
}

onFragmentViewCreated is called just fine, the adapters getItem is not...

Upvotes: 1

Views: 115

Answers (1)

StuStirling
StuStirling

Reputation: 16231

It may be how you're navigating between the bottom navigation fragments and subsequently whats happening with their lifecycle.

If you are using .replace on the FragmentTransaction, try using .show and .hide instead.

Something like the following might do it:

public void navigateToFragment( Fragment frag ) {
     FragmentTransaction transaction = getFragmentManager().beginTransaction();

    transaction.hide(currentFragment);

    if ( !fragment.isAdded() ) {
        transaction.add(R.id.container,fragment)
    } else if ( !fragment.isHidden() ) {
        transaction.show(fragment);
    }
    transation.commit();
}

Upvotes: 1

Related Questions