Christer Nordvik
Christer Nordvik

Reputation: 2528

Black empty fragments when using nested fragments with ViewPager (Android 4.2, support lib)

I have an app using SlidingMenu, Action Bar Sherlock and now I am trying to rewrite my previous code to using the nested fragments introduced in support lib with the release of Android 4.2.

The problem is that when I swipe through the fragments, they are visible for a split second and then they disappear (black screen). Sometimes they stay on screen, and sometimes the screen is entirely black and the fragment doesn't appear at all. I suspect I have a lack of understanding of the fragment lifecycle, but I really can't see the error here.

Swiping through them gets me different results. On initial load the first fragment is black. If I swipe to nr 4 and back to nr 1, then nr 1 fragment is OK. If I swipe fast to nr 2 and then to nr 1, the nr 1 is black again. If I swipe slowly from nr 2 to nr 1, then nr 1 stays OK :-/

Here is my code:

public class LeagueInfoFragment extends SherlockFragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container != null) {
        container.removeAllViews();
    }
    setRetainInstance(true);
    setHasOptionsMenu(true);
    mExampleId = getArguments().getInt("mExampleId");

    View view = inflater.inflate(R.layout.viewpager_tournament, container,
            false);
    ViewPager awesomePager = (ViewPager) view.findViewById(R.id.pager);
    awesomePager.setAdapter(new ExamplePagerAdapter(this, getChildFragmentManager(), createFragments()));

    TabPageIndicator titleIndicator = (TabPageIndicator) view
            .findViewById(R.id.titles);
    titleIndicator.setViewPager(awesomePager);

    return view;
}

private List<Fragment> createFragments() {
        List<Fragment> list = new ArrayList<Fragment>();
        list.add(LeagueTableFragment.newInstance(0));
        list.add(FixturesFragment.newInstance(1));
        list.add(NewsFragment.newInstance(2));
        list.add(TopscorerFragment.newInstance(3));
        return list;    
}

private class ExamplePagerAdapter extends FragmentPagerAdapter {
    String[] pages;

    LeagueInfoFragment _activity;
    List<Fragment> fragments;
    private ExamplePagerAdapter(LeagueInfoFragment a,
            FragmentManager fragManager, List<Fragment> frags) {
        super(fragManager);
        this.fragments = frags;
        _activity = a;
        pages = new String[] { _activity.getString(R.string.table),
                _activity.getString(R.string.fixtures),
                _activity.getString(R.string.news),
                _activity.getString(R.string.top_scorers) };
    }

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

    @Override
    public Fragment getItem(int arg0) {
        return fragments.get(arg0);         
    }

    public String getPageTitle(int position) {
        return pages[position];
    }
}

And here is one of my fragments (simplified to reduce code posted here):

public class NewsFragment extends SherlockFragment implements IAsynchData
{   
public static NewsFragment newInstance(int notUsed) {
    NewsFragment f = new NewsFragment();
    return f;
}

@Override
public View onCreateView(android.view.LayoutInflater inflater,
    ViewGroup container, Bundle savedInstanceState) {
    if (container != null) {
        container.removeAllViews();
    }
    View view = inflater.inflate(R.layout.fragment_news, container,
            false);
            //Code left out to load the URL in the webview
    updateRssFeed("http://www.sportspage.com/myteam");
    return view;
}   

}

Upvotes: 1

Views: 1841

Answers (1)

nfirex
nfirex

Reputation: 1523

Sorry, but why are you doing this while onCreateView in Fragment:

if (container != null) {
        container.removeAllViews();
}

As I know, in this moment, container is your ViewPager and removeAllViews() - remove all created View of your Fragments in ViewPager.

Upvotes: 2

Related Questions