Hamburger icon not showing correctly after toolbar replacement

Viewed 420

I have an activity that has a fragment layout dedicated to the toolbar. In the center we have a normal nav_host container.

The activity contains one fragment that correctly shows the hamburger. If I click a button it gets replaced from a second fragment that replace the old toolbar with a new one. After clicking back the new toolbar is replaced by the old one.

The problem is that the hamburger icon doesn't show up and the toolbar doesn't work as expected. Ideas?

This is the code in the activity:

open fun setupToolbar() {
    initialToolbar = ToolbarFragment()
    setToolbarFragment(initialToolbar){
        setNavController()
    }
}

fun setToolbarFragment(fragment: Fragment, callback: () -> (Unit) = {}) {
    supportFragmentManager
            .beginTransaction()
            .replace(R.id.toolbarContainer, fragment)
            .runOnCommit {
                callback.invoke()
            }.commit()
}

private fun setNavController() {
    val navController = findNavController(R.id.nav_host_fragment)
    val appBarConfiguration = setFragmentsWithHamburgerMenu(navController)
    toolbar.setupWithNavController(navController, appBarConfiguration)
}

fun setFragmentsWithHamburgerMenu(navController: NavController): AppBarConfiguration {
    return AppBarConfiguration(
            setOf(
                    R.id.analyticsFragment,
                    R.id.routinesFragment,
                    R.id.currentRunFragment,
                    R.id.myMapsFragment,
                    R.id.myRobotsFragment
            ),
            drawer_layout
    )
}

This is the code instead of the second fragment:

override fun onResume() {
    super.onResume()
    val fragment = SearchToolbarFragment(searchHint, this::onQueryTextChange)
    baseActivity.setToolbarFragment(fragment) {
          // other stuff
    }

}

override fun onPause() {
    super.onPause()
    baseActivity.setupToolbar()
}

This instead is my activity layout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable name="viewModel" type="[package].DashboardViewModel" />
    </data>

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/toolbarContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"/>

            <fragment
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:id="@+id/nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbarContainer" />

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_nav"
                style="@style/BottomNavigationView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                app:itemBackground="@color/white"
                app:itemTextColor="@color/button_view_item"
                app:labelVisibilityMode="labeled"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:menu="@menu/menu_bottom_nav" />

            <LinearLayout
                android:id="@+id/progressMaskLayout"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbarContainer">

                <include layout="@layout/progress_mask" />
            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true">

            <include
                bind:viewModel="@{viewModel}"
                layout="@layout/element_navigation_bar"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </com.google.android.material.navigation.NavigationView>
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>
2 Answers

To avoid the problem I just added a fragment over the previous one and I removed the added one.

Like this:

fun addToolbarFragment(fragment: Fragment, callback: () -> (Unit) = {}) {
    supportFragmentManager
            .beginTransaction()
            .add(R.id.toolbarContainer, fragment)
            .runOnCommit {
                callback.invoke()
            }.commit()
}

fun removeToolbarFragment(fragment: Fragment, callback: () -> (Unit) = {}) {
    supportFragmentManager
            .beginTransaction()
            .remove(fragment)
            .runOnCommit {
                callback.invoke()
            }.commit()
}

In this way, all the customization done in the first toolbar won't be lost.

First, you're using Android jetpack navigation component but also using fragment manager to manually do fragment transactions. You're defying the purpose of using the jetpack navigation component. That's the whole point of using the navigation library so that we don't have to manually do the fragment transactions. What you have to do here is put your toolbar in the main_activity.xml so that all fragments can inherit it.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<layout 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">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/Drawer_Main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/Layout_Coordinator_Main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/Toolbar_Main"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary">

                <TextView
                    android:id="@+id/Toolbar_Main_Title"
                    style="@style/Locky.Text.Toolbar.TitleText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/app_name" />

            </com.google.android.material.appbar.MaterialToolbar>

            <androidx.core.widget.NestedScrollView
                android:id="@+id/Nested_Scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="?attr/actionBarSize"
                android:fillViewport="true">

                <fragment
                    android:id="@+id/Navigation_Host"
                    android:name="androidx.navigation.fragment.NavHostFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:defaultNavHost="true"
                    app:navGraph="@navigation/navigation_drawer_main" />

            </androidx.core.widget.NestedScrollView>
            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/FAB_Add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_margin="@dimen/fab_margin"
                app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
                app:srcCompat="@drawable/ic_add" />

        </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/Navigation_View"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:clipToPadding="false"
            android:paddingStart="0dp"
            android:paddingEnd="16dp"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/menu_drawer_main" />

    </androidx.drawerlayout.widget.DrawerLayout>

</layout>
Related