2

I have an activity which makes use of a FragmentTabHost, so each tab has a fragment inside it. In the first tab, I have 2 nested fragments, one on the top half of the screen, the other on the bottom half. The fragment on the bottom uses a ViewPager to scan through several LinearLayouts. It all works pretty well.

Until you move to another tab, and return to the first. The bottom nested fragment no longer appears, but the one on top does.

Here's some code demonstrating my usage:

This is how I'm using my main activity:

public class MainActivity extends SherlockFragmentActivity
{
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity_layout_vert);

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 

        mTabHost.addTab(mTabHost.newTabSpec("TAB1").setIndicator("", getResources().getDrawable(R.drawable.tab1_selector)), Tab1Fragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("TAB2").setIndicator("", getResources().getDrawable(R.drawable.tab2_selector)), Tab2Fragment.class, null);
    }
}

Here's how I'm using the fragment for tab1:

public class Tab1Fragment extends SherlockFragment
{
    private NestedFragment1 mNestedFrag1 = null;
    private NestedFragment2 mNestedFrag2 = null;


    @Override
    public void onCreate(Bundle inSavedInstanceState)
    {
        super.onCreate(inSavedInstanceState);

        mNestedFrag1 = new NestedFragment1();
        mNestedFrag2 = new NestedFragment2();

        FragmentManager fragManager = getChildFragmentManager();
        FragmentTransaction fragTransaction = fragManager.beginTransaction();

        fragTransaction.add(R.id.nested_frag1_container, mNestedFrag1, "Frag1");
        fragTransaction.add(R.id.nested_frag2_container, mNestedFrag2, "Frag2");
        fragTransaction.commit();
    }

    @Override
    public void onDestroy()
    {
        FragmentManager fragManager = getChildFragmentManager();
        FragmentTransaction fragTransaction = fragManager.beginTransaction();

        fragTransaction.remove(mNestedFrag1);
        fragTransaction.remove(mNestedFrag2);
        fragTransaction.commit();
    }
}

And here is how I'm using the 2nd nested fragment (the one that disappears):

public class NestedFragment2 extends SherlockFragment
{
    private MyPageAdapter mPageAdapter;
    private ViewPager mViewPager;

    @Override
    public void onCreate(Bundle inSavedInstanceState)
    {
        super.onCreate(inSavedInstanceState);

        mPageAdapter = new MyPageAdapter();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {   
        View toReturn = inflater.inflate(R.layout.nested_fragment1_layout_top, container, false);

        mViewPager = (ViewPager)toReturn.findViewById(R.id.viewpager);
        mViewPager.setAdapter(mPageAdapter);
        mViewPager.setOffscreenPageLimit(5);

        return toReturn;
    }

    private class MyPageAdapter extends PagerAdapter
    {
        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {           
            View page = getActivity().getLayoutInflater().inflate(R.layout.view_pager_controls_layout, container, false);

            container.addView(page);

            return page;
        }

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

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

        @Override
        public float getPageWidth(int position)
        {
            return 1;
        }

        @Override
        public boolean isViewFromObject(View view, Object object)
        {
            return(view == object);
        }
    }
}

Thoughts?

fllo
  • 11,595
  • 5
  • 39
  • 95
KairisCharm
  • 1,273
  • 1
  • 11
  • 31

3 Answers3

0

Sounds as if it is being destroyed when you move to a different tab. Try setting ViewPager.setOffscreenPageLimit() to a high number and see if it still happens.

Dororo
  • 3,370
  • 2
  • 27
  • 46
  • I'm already setting it to 5. Is it possible you misunderstand? The viewpager has nothing to do with the tabs. It is used in a nested fragment inside the first tab. – KairisCharm Jun 19 '13 at 21:21
  • I would need to see how you're implementing the 2 nested fragments. – Dororo Jun 19 '13 at 21:25
0

Most likely it's because you should be using a childFragmentManager for the nested fragments, instead of the one obtained from the activity.

From a "top level" fragment whenever you gonna do the transaction to include the nested fragment use getChildFragmentManager() instead of getFragmentManager()

Budius
  • 37,587
  • 15
  • 92
  • 134
0

I figured it out. The above code was not the problem. The problem had to do with the way I was handling the PagerAdapter inflation. I wasn't doing it exactly how I showed above. The way I was actually using it was preventing an inflation of the views from happening.

KairisCharm
  • 1,273
  • 1
  • 11
  • 31