What I have:
I am using the latest Android support and design libraries on API 22. I have a fragment called ProgressFragment which I only run when my app is first installed as follows:
mProgressFragment = new ProgressFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, mProgressFragment, TAG_TASK_FRAGMENT)
.commit();
This is the layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progress_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<ProgressBar
android:id="@+id/build_lib_progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progress_msg1"
android:layout_gravity="center"
android:textSize="22sp"
android:textColor="@color/text_primary"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progress_msg2"
android:layout_gravity="center"
android:paddingTop="10dp"
android:textColor="@color/text_secondary"/>
</LinearLayout>
</LinearLayout>
This fragment contains an AsyncTask. When it completes, it is replaced by another fragment (called MainFragment) in MainActivity inside the AsyncTask callback onPostExecute() like so:
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, new MainFragment())
.commit();
This is the MainFragment layout:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/main_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.AppBarLayout
android:id="@+id/main_appbar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<include layout="@layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="@+id/main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorHeight="4dp"
app:tabTextColor="@color/white"/>
</android.support.design.widget.AppBarLayout>
EDIT:
And this is MainActivity's layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Main content displayed here -->
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Navigation Drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_drawer_header"
app:menu="@menu/nav_view_menu"
android:background="#ffffff"
app:itemTextColor="@color/text_tertiary"
app:itemIconTint="#8b8b8b"
/>
The Problem:
When the AsyncTask in ProgressFragment completes and is replaced by MainFragment, the TabLayout view does not display tabs:
My application has 2 possible paths at startup. It either:
Adds the ProgressFragment (if first run) -> then replaced by MainFragment
Adds MainFragment otherwise (tabs show up fine here)
Both Fragment replacements happen in MainActivity.
The problem deals with path 1
EDIT 2:
Calling recreate() in MainActivity "fixes" this for now. Though I wouldn't call it a solution. Will come back to it later.