Why is changing fragments hiding my tablayout (android)?
I have two fragments in my application. fA and fb. fA has a toolbar, tab layout and viewer. fB has text representation and image. When I click on any button in fA on any pager, it opens fB and displays an image with some text. It opens everything fine, but when I click back to go back to my first snippet, I lose the tab layout. Space for tabs is present, but there is no indicator or title. Can anyone please help?
My fA xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:clipToPadding="false"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
android:clipToPadding="false"
android:elevation="4dp" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
android:clipToPadding="false"
android:elevation="4dp"
android:paddingEnd="0dp"
android:paddingStart="48dp"
android:layout_marginTop="56dp"
android:scrollbars="horizontal"
app:tabGravity="center"
app:tabIndicatorColor="@android:color/white"
app:tabMode="scrollable"
app:tabTextColor="@color/primary_text" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</FrameLayout>
and my class:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
titles = getTitles();
MainActivity parent = (MainActivity) getActivity();
parent.setToolbar((Toolbar) view.findViewById(R.id.toolbar));
viewPager = (ViewPager) view.findViewById(R.id.viewPager);
stateListener = parent.getViewPagerStateListenerInstance();
viewPager.setAdapter(new PagerAdapter(getChildFragmentManager()));
viewPager.addOnPageChangeListener(stateListener);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
parent.setUpTabLayout(tabLayout);
}
@Override
public void onDestroy() {
super.onDestroy();
viewPager.removeOnPageChangeListener(stateListener);
}
My toolbar and tab layout are in a snippet and need to be replaced.
Any help is appreciated. Thank you.
+3
source to share