lionelmessi
lionelmessi

Reputation: 1144

SlidingTabLayout Pager fragments are vanishing after parent Fragment onPause() is called

My App is designed like: A, B, C and D fragments.

|  A  |  B  |
         ↓
   |  C  |  D  |

A and B are top level Tablayout(TL1) fragments. I have another Tablayout(TL2) in B which holds C and D.

Both TL1 and TL2 work fine the first time I open the App. After sometime, when I switch to fragment A and come back to B, I can only see TL2 tab headings but the fragment contents are missing.

I dont see this behaviour with TL1 which is part of main activity. I am suspecting something to do with onPause() call of fragment B and when returning back it is not able to find the contents of TL2.

My fragment xml is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<com.example.rv.android.ui.widget.SlidingTabLayout
    android:id="@+id/slidingProfileTabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="scrollable"/>

<android.support.v4.view.ViewPager
    android:id="@+id/vpProfileTab"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@android:color/white"/>

I am setting up the tabs in my fragment onCreateView() like

    private void setupTabs() {

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    vpProfileTab.setAdapter(new MyProfileTabFragmentPagerAdapter(getActivity()
            .getSupportFragmentManager(),
            ctx));

    // Give the TabLayout the ViewPager
    slidingProfileTabs.setDistributeEvenly(true);
    slidingProfileTabs.setBackgroundColor(colorAccent2);
    slidingProfileTabs.setSelectedIndicatorColors(colorTabIndicator);
    slidingProfileTabs.setViewPager(vpProfileTab);
}

Each of the tabs are fragments in themselves. My page adapter is:

public class MyProfileTabFragmentPagerAdapter extends FragmentPagerAdapter {

private String tabTitles[];
private Context context;
private static final String TAG = makeLogTag(MyProfileTabFragmentPagerAdapter.class);

public MyProfileTabFragmentPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    tabTitles = context.getResources().getStringArray(R.array.profileTabs);
    this.context = context;
}

@Override
public int getCount() {
    return tabTitles.length;
}

@Override
public Fragment getItem(int position) {
    LOGI(TAG, "Info tab" + position);

    switch (position){
        case 0:
        {

            return new UserProfileFragment();
        }
        case 1:
        {

            return new FeedFragment();
        }

        default:
        {
            LOGI(TAG, "Invalid tab" + position);
            return null;
        }
    }
}

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    return tabTitles[position];
}}

Any suggestions on how to proceed forward?

Upvotes: 0

Views: 54

Answers (1)

RussHWolf
RussHWolf

Reputation: 3654

When initializing your ViewPager, instead of getActivity().getSupportFragmentManager(), try using the fragment getChildFragmentManager() method.

Upvotes: 1

Related Questions