I've got a running Android App with a Navigation Drawer Activity. For every menu item, I wanted to implement a separate Fragment, so I can use the same toolbar and drawer menu. Now one of this Fragments should contain a view with 3 Tabs (3 Fragments with Recyclerviews in them). I created a Tabbed Activity and migrated the code in a new Fragment. When I click on the menuitem with the Tab-Fragment for the first time, it works perfectly fine. But when i browse through the menu and then open the Tab-Fragment again, it shows the 3 Tabs but there's nothing in them. Now when I click on the third Tab, the data is shown. And when I switch back to the first Tab, the Data is back, too. Only the middle Tab stays empty.
I tried this method on a completely new and empty project, only with a navigation drawer activity and an tabbed activity, where I also migrated the code to a fragment. I only used the code which Android-Studio (3.4.2) generated. In these tabs are only textviews, but there's still the same issue.
Tab-Fragment (migrated code from a generated Tabbed Activity):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_tab, container, false);
context = getActivity().getApplicationContext();
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(context, getActivity().getSupportFragmentManager());
ViewPager viewPager = v.findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = v.findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
return v;
}
Generated SectionsPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
@StringRes
private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2};
private final Context mContext;
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return "Some Title";
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
I only changed the amount of Tabs in "getCount" and the returned String in "getPageTitle"
I handle the menu item clicks in my Navigation Drawer Activity (Main) with the NavigationItemSelectedListener and change the Fragment like this:
getSupportFragmentManager().beginTransaction().replace(R.id.content, new TabFragment()).commit();
The "activity_tab" layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Is there a possibility to stay with Fragments on menu item clicks or do I have to use a Tabbed Activity?
First of all, I think you should use childFragmentManager instead of supportFragmentManager.
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(context, getChildFragmentManager());