windchime
windchime

Reputation: 1285

Fragment does not display any content

I am working on an android project. I found a strange behavior of viewpager and fragments. Once more than a few fragments in a viewpager are loaded, some of the fragments do not display any content at all for a period of time, usually more than 10 seconds. After that, the contents are displayed correctly again.

Here is what I have:

Its UI uses a MainActivity which only contains a viewpager mMainPager.

    private void setupViewPager() {
        mMainPageAdapter = new MainPageAdapter(getSupportFragmentManager());
        mMainPager.setAdapter(mMainPageAdapter);
        mMainPager.addOnPageChangeListener(this);
        mMainPager.setOnSwipeOutListener(this);
    }

MainPageAdapter is defined as follows inside MainActivity. It has 4 fragments:

private class MainPageAdapter extends FragmentStatePagerAdapter {

    public MainPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUMBER_OF_PAGES;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return Fragment0.newInstance();
            case 1:
                return Fragment1.newInstance();
            case 2:
                return Fragment2.newInstance();
            default:
                return Fragment3.newInstace();
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }
}

Fragment3 has a viewpager nested inside. That viewpager contains 4 fragments.

  ChatsViewPager pagerChats;

The adapter uses com.ogaclejapan.smarttablayout.utils.v4

    FragmentStatePagerItemAdapter adapterSegChat = new FragmentStatePagerItemAdapter(getChildFragmentManager(), FragmentPagerItems.with(getActivity())
            .add(getString(R.string.string0), Fragment30.class)
            .add(getString(R.string.string1), Fragment31.class)
            .add(getString(R.string.string2), Fragment32.class)
            .add(getString(R.string.string3), Fragment33.class)
            .create());


    pagerChats.setAdapter(adapterSegChat);

Once MainActivity is launched, Fragment 2 was displayed. If user swipes to Fragment3, all four fragments in pagerChats display correctly. Swiping can smoothly transition between those 4 fragments.

However, once the user swipes to Fragment0 and Fragment1 and then back to Fragment3, 2 of the four fragments, Fragment32 and Fragment33 in pagerChats are no longer displaying. No error, no freezing. They simply display a blank screen.

After user waits for more than 10 seconds, the content of Fragment32 and Fragment33 shows up.

If user repeat the process above, the same thing happens again.

This is using Samsung S7, running API 24.

Why it is like this? Is it due to memory constraint?

Upvotes: 1

Views: 134

Answers (1)

You're right: memory constraint. Not directly has to do with your specific device label or API. See this: https://stackoverflow.com/a/12538927/5885018

Upvotes: 2

Related Questions